I am confused with the behaviour of self when it comes to dealing with static variables in python.From what I understand is that static variables can be accessed by either using
bean.mycar = "yup"
print(self.mycar) #prints yup
self.mycar = "pup"
Both classes and instances can have attributes.
A class attribute is assigned to a class object. People sometimes call this a "static variable".
An instance attribute is assigned to an instance ("instance variable").
When an attribute of an object is read, a number of things happen (see Descriptor HowTo Guide), but the short version is:
When it is written, then there is no such mechanism. It is written where it is written ;)
See in example:
class A(object): pass a = A() print A.value # fails - there is no "value" attribute print a.value # fails - there is no "value" attribute A.value = 7 print A.value # prints 7 print a.value # also prints 7 - there is no attribute on instance, but there is on class a.value = 11 print A.value # prints 7 print a.value # prints 11 - now there is an attribute on the instance a2 = A() print a2.value # prints 7 - this instance has no "value", but the class has
self argument (in the question) is an instance, just like
a is here.