#๐ I am trying to learn python and now its time for a chatting system
93 messages ยท Page 1 of 1 (latest)
@charred ibex
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.
I geniuenly need help
what are classes
wait
I need to begin from scratch
can someone please explain
what is self
omfg
i made a small post about oop here
you can check it out if youd like
Self is a way for a class to see itself ;)
I am trying to learn
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
you dont need to pass in any argument to the printInfo method
WHAT
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
so like an object no? like defining a new person, giving it height and weight
like that right?
but what are init and
fucking self
like wth is that
why
Init is just a function, python calls it when you create a class instance
so init has no meaning?
Init has meaning
i kinda reccomend you reading this https://discord.com/channels/267624335836053506/1235883588206202942
it has everything you need to know
tbh
alright let me check that out
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 doself.name = nameyou'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.
Try modifing and play around with the little codes I've given as examples and get a hang of those stuff
yeah sort of got it...
If you still have some doubts you can ask
how do I get the person's name?
eg
I got a question
I figured it out but
why do we
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
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
No, the other way around
OOOOOOOOOOOH
Argument is stored in whatever
it has to do with scope
nice
lmao
๐ฅ
super() is used while subclassing
super() basically allows you to call methods from parent class
thats ALL it does
nothing else
can't we call it without super wtf?
what are methods
like init
well you can kinda
?
yes
thats a special method
it has its own name called dunder methods
and methods are basically functions inside classes
their name just changes
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
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.