What is going on with x.brand1?


#1

Hi,

I am confused why line 47 prints out “Alan” instead of “Thales”. If this is the case, why doesn’t line 46 print “Kuka”?
My reasoning is that x and y are instances of the class Robot, by updating the class variable brand1 to “Thales” on line 45, it shouldn’t affect x.brand1 and y.brand1 because these are their OWN attribute, not the class, but the next instance that we create should have the property that brand1=“Thales”.

Where did i go wrong?
Thanks! :):grinning:


#2

After further investigation,

print(y. __dict __) prints a empty dictionary. This also confuses me. The Robot class was given the variable brand1 on line 42. Then on line 43 I created the y Robot object. So y also has to have the attribute brand1. What’s going on?


#3

Classes do not have attributes, only methods. The instances have the attributes.


#4

Is this what’s going on?

Robot.brand1 is just a variable inside the class. It is not an attribute that it’s objects get. Is it something like this?

class Robot():

def __init__(self):
    # I didn't specify any attributes


brand1 = "Kuka"

#5
>>> class Robot():
    brand1 = "Kuka"
    def __init__(self):
        pass

>>> x = Robot()
>>> x.brand1
'Kuka'
>>> Robot.brand1
'Kuka'
>>> x.brand1 = "Luka"
>>> x.brand1
'Luka'
>>> Robot.brand1
'Kuka'
>>> 

I should say that classes don’t have attributes as such, but class variables which behave like attrbiutes. Every instance inherits the same one from the parent class, but can alter it at any time after instantiation. Once altered, it becomes an instance attribute, no longer tied to the parent.

To further add to the confusion,

>>> Robot.brand2 = "Marc"
>>> Robot.brand2
'Marc'
>>> x.brand2
'Marc'
>>> 

Notice that we are able to instantly give every instance a new attribute just by giving it to the parent. I’m still kind of bent on the term “class variable” as opposed to “class attribute”, probably because it is necessary to keep a distinction between class and instances (in my own mind, perhaps).

Bottom line, if we give a new attribute to an instance, it is isolated to that instance. If we give a new attribute to the parent class, it immediately descends to all instances of that class. Each instance can alter it (make it an instance attribute) as needs be.

>>> x.brand3 = "Mica"
>>> Robot.brand3 = "Formica"
>>> x.brand3
'Mica'
>>> y = Robot()
>>> y.brand3
'Formica'
>>> 

Notice that the earlier declared attribute on x is unchanged when Robot declares a variable of the same name? All other instances will be affected, though, unless they too have this attribute, already.

One final note… A class variable is excellent for initializing counters and other preset objects on instances that they can individually update. Recall that once an instance changes the value it becomes isolated to that instance.

Class variables are also useful for keeping a running count of, and/or identifying instances; i.e., giving every instance a unique id. This could not be done from a program line (on the fly), though. It would have to happen in the __init__ method and would use a class method to do it, most likely.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.