#🔒 Need help with understanding classes and attributes

71 messages · Page 1 of 1 (latest)

rich tulip
#

What is the difference between an instance attribute (ex. Attributes made under the init method), and just methods created under the class? Are they not the same thing because they are called upon using the . Operator? It may take me a bit to respond as I have to go somewhere rn but ty in advance to anyone who helps

gilded berryBOT
#

@rich tulip

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.

dim forge
strange gulch
#

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

rich tulip
strange gulch
#

bad english

dim forge
rich tulip
dim forge
#

instance attributes are unique to the instance

dim forge
rich tulip
dim forge
#

that includes methods. Methods are also attributes

dim forge
strange gulch
#

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

rich tulip
sand iron
#

"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.

dim forge
#

if I say "make a new attribute", I'm not usually saying "make a new method"

strange gulch
#

yeah im still kinda confused on the topic my self lol

dim forge
#

but yes, methods are also attributes

rich tulip
dim forge
#

correct

rich tulip
#

I see

strange gulch
dim forge
#

just like all methods are functions, but not all functions are methods

dim forge
strange gulch
# dim forge how so?

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

rich tulip
#

And you said creating non method attributes under anything other than the init method is bad practice?

dim forge
#
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

strange gulch
dim forge
#

for OP

rich tulip
dim forge
#

class attribute*, yes

rich tulip
#

It’s shared amongst all instances

#

Alr

dim forge
#

I don't need or want each instance to have its own top speed attribute

sand iron
# dim forge how so?

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")

dim forge
#

it's something I want to determine class-wide

rich tulip
#

So anything under init, or an attribute specific to an instance is called an instance attribute?

strange gulch
#

@dim forge can you explain elif statements though it's like the only thing i don't understand i don't know why

strange gulch
#

oh thanks

dim forge
#

"Would you like chocolate?"

dim forge
#

you say yes

rich tulip
#

Or fashoomp prolly better at explaining than me listen to him

sand iron
strange gulch
dim forge
rich tulip
#

@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

strange gulch
dim forge
rich tulip
dim forge
rich tulip
#

I’ll learn something too I didn’t know maybe

dim forge
#

but sure, if s doesn't mind keeping it open

strange gulch
#

alr

dim forge
# strange gulch uh-huh

If you already said you wanted chocolate, wouldn't it be odd for me to keep reading flavours to you?

strange gulch
#

ohhh

rich tulip
#

He started new thread I’ll js close this

dim forge
#

ok

rich tulip
#

!close

gilded berryBOT
#
Python help channel closed with !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.