#🔒 Need help with understanding classes and attributes
71 messages · Page 1 of 1 (latest)
@rich tulip
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.
do you understand the difference between class and instance?
I like to think as classes as i blueprint to a house, the objects/attributes are just building pieces to that house. Essentially, making a class called monster that can growl and bite, but you have 5 different monsters running around
if that makes sense im terrible at explaining
Yes a class is like a blueprint while an instance is an object created from that class
bad english
and that's exactly the difference between those attributes. Class attributes are values meant to be shared amongst all instances
yeah exactly
How does attribute differ from a function/method or wtv u call it made inside a class ?
instance attributes are unique to the instance
an attribute is "any data accessible through an instance"
Are class attributes strictly only made under the initiation method?
that includes methods. Methods are also attributes
They don't have to be but it would be unwise not to
attributes are just the labels that say what an object is like a player's health or a car's color they are just pieces of info sitting there while methods are the actions that make the object do things like jump or run you can tell them apart because methods have parentheses at the end since they are actually working to change your attributes or move stuff around
That’s what I’m confused on because I’m reading documents and watching videos but they refer to them as different things
"created under" is not precise enough language to be able to answer the question.
. doesn't "call" things; it only looks them up (or, in a case like x.y = z, explains what to set).
When you look something up on an object, and it isn't there, Python may also look in that object's class.
If Python finds something in the class instead of on the object itself, and the thing it finds is a function, then it will create a method. That happens fresh every time.
Calling the method happens using the () part of the syntax.
it's both things
if I say "make a new attribute", I'm not usually saying "make a new method"
yeah im still kinda confused on the topic my self lol
but yes, methods are also attributes
All methods are attributes but not all attributes are methods
correct
I see
technically yes, but no
just like all methods are functions, but not all functions are methods
how so?
I think I get it now
the main thing to remember is that a method is just an attribute that you can run by putting those () brackets at the end if you forget the brackets it won't do the action it just tells you that the function exists
And you said creating non method attributes under anything other than the init method is bad practice?
class Car:
top_speed = 200
def __init__(self, make, model):
self.make = make
self.model = model
have a look at this example
top_speed is an attribute of the class
it's a value I want all instances to respect
this for me or the guy asking the question
for OP
top_speed is a class variable right
class attribute*, yes
I don't need or want each instance to have its own top speed attribute
there's some ambiguity in definitions. When you make a method call on an object, the method that gets called isn't physically part of the object; it's created on the fly, from the function that was looked up; and that function is physically part of the object's class, not the object itself.
("physically" in the sense of "in memory, the data representing the object includes bytes that directly reference the attribute")
it's something I want to determine class-wide
So anything under init, or an attribute specific to an instance is called an instance attribute?
@dim forge can you explain elif statements though it's like the only thing i don't understand i don't know why
I can help with that
oh thanks
Let's say you're at the store ordering ice cream and I begin reading out flavour options to you
"Would you like chocolate?"
ok...
you say yes
Or fashoomp prolly better at explaining than me listen to him
Better to use a separate thread if you have your own unrelated question.
uh-huh
I should probably stop reading you flavours because you said yes already, correct?
@dim forge after you help him I think this post is done I understand the difference now but I’ll keep it open till u finish helping him
its not that big of a deal bro
Sorry yes, we can move into a new thread
Na alg
We like to keep threads on topic
I’ll learn something too I didn’t know maybe
but sure, if s doesn't mind keeping it open
alr
If you already said you wanted chocolate, wouldn't it be odd for me to keep reading flavours to you?
ohhh
He started new thread I’ll js close this
ok
!close
This help channel has been closed. 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.