In below example Test class has two instance method and one classmethod
In set_cls_var_1 method I set class variable using self.
In set_cls_var_2 method I call class method using self.
class Test():
#class variable
cls_var = 10
def __init__(self):
obj_var=20
def set_cls_var_1(self,val):
#second method to access class variable
print "first "
self.cls_var = val
def set_cls_var_2(self):
print "second"
self.task(200)
@classmethod
def task(cls,val):
cls.cls_var = val
t=Test()
#set class variable by first method
t.set_cls_var_1(100)
print Test.cls_var
#set class variable by second method
t.set_cls_var_2()
print Test.cls_var
Output
first
10
second
200
Expected Output
first
100
second
200
My question is: why only classmethod can call by self, Why not class variable
When you attempt to access an object's attribute using self
, Python first searches the object's attributes. If it cannot find it there, then is searches the object's class's attributes. That is what's happening in your case;
Python first searches t
's attributes. It doesn't find cls_var
, so it then searches the T
class's attributes. It finds cls_var
so it stops, and returns cls_var
's value.
However, when assigning attributes to self
, Python always assigns them directly to the object, and never the object's class unless explicitly told to do so. That's why assinging self.cls_var
to 100
didn't affect Test
's cls_var
attrbiute.