Access Class method and variable using self

Viewed 22047

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

3 Answers
Related