#๐Ÿ”’ I am trying to learn python and now its time for a chatting system

93 messages ยท Page 1 of 1 (latest)

charred ibex
#

Idk where to start and documentations fucked my brain up already, I would love it if someone leads me

rain trailBOT
#

@charred ibex

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

charred ibex
#

I geniuenly need help

charred ibex
#

what are classes

#

wait

#

I need to begin from scratch

#

can someone please explain

#

what is self

#

omfg

bronze charm
#

i made a small post about oop here

#

you can check it out if youd like

minor glade
charred ibex
#
class Person:
    def __init__(self, name, age):
        self.a = name
        self.b = age
    def printInfo(self):
        print(self.name)
        print(self.age)

username = str(input("Name?"))
userage = int(input("Age?"))

newuser = Person(username, userage)
Person.printInfo(newuser)```
#

what the hell is this shit

#

It doesn't work

#

what

#

Im getting a brainfuck

bronze charm
#

you dont need to pass in any argument to the printInfo method

charred ibex
#

WHAT

bronze charm
#

because it doesnt take in any paramters

#

self is a type of default parameter

#

its used to access attributes and methods from within the class

#

but its kind of hidden when you use it from outside the class

charred ibex
#

like that right?

#

but what are init and

#

fucking self

#

like wth is that

#

why

minor glade
#

Init is just a function, python calls it when you create a class instance

charred ibex
#

so init has no meaning?

minor glade
#

Init has meaning

bronze charm
#

it has everything you need to know

#

tbh

charred ibex
#

alright let me check that out

bronze charm
# bronze charm i kinda reccomend you reading this https://discord.com/channels/2676243358360535...
class Person:
    def __init__(self, name, age, net_worth):
        self.name = name
        self.age = age
        self.net_worth = net_worth

Let's go step by step. First we created a __init__() method which allows us to make the "template". It's first parameter will always be self (not entirely true but you can assume it as so). self represents the current instance of the class, so while you do self.name = name you're binding an attribute name to a instance of a class named Person. Which means we're creating a new copy of the Person class (which is called instance) and storing attributes in that new copy.

bronze charm
bronze charm
#

If you still have some doubts you can ask

charred ibex
#

how do I get the person's name?

#

eg

#

I got a question

#

I figured it out but

#

why do we

bronze charm
#

If you want to access it from within the class you do self.name and if you're accessing from outside you do classintance.name

charred ibex
#

do self.whateverwewant = arguement but when we call it we do self.whateverwewant instead of self.arguement?

#

I mean isn't it stored inside arguement?

#

python is weird

minor glade
#

No, the other way around

charred ibex
#

OOOOOOOOOOOH

minor glade
#

Argument is stored in whatever

charred ibex
#

WAI

#

T

#

NOW

#

I GOT IT

bronze charm
bronze charm
charred ibex
#

FUCKING WEIRD ASS SHIT

#

MADE ME GO CRAZY

#

(almost)

bronze charm
#

lmao

charred ibex
#

what

#

now I am trying to make a chatting system

#

its gonna make me go bald ngl

bronze charm
#

๐Ÿ”ฅ

charred ibex
#

WTH IS SUPER

#

super()

#

WHAT

bronze charm
#

super() basically allows you to call methods from parent class

#

thats ALL it does

#

nothing else

charred ibex
#

what are methods

#

like init

bronze charm
#

well you can kinda

charred ibex
#

?

bronze charm
#

thats a special method

#

it has its own name called dunder methods

#

and methods are basically functions inside classes

#

their name just changes

charred ibex
#

oh like uhm

#

def changeUsername(): inside a class?

bronze charm
#

yeah

#

that would be a method

bronze charm
# charred ibex ?

oh btw to answer to this, when youre subclassing you automatically inherit parent methods also

#

but in some cases in the subclass you may overwrite the parent method but you also want to call the orignal method at the same time, so youd use super() in that case

rain trailBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.