Search code examples
pythonstatic-methodsinstance-variablesclass-method

Why is class variable accessible via class instance?


I have created class Circle with no instance variables. I've added a class-method from_diameter to generate the circle with given diameter.

class Circle:

    @classmethod
    def from_diameter(cls, diameter):
        cls.diameter = diameter
        return cls

diameter is a class (static) variable. However - it looks like it exists as an instance variable and class variable as well.

myCircle = Circle.from_diameter(10)
print Circle.diameter
print myCircle.diameter

Output:

10
10

Why does it work? There is no instance variable diameter. I suppose print myCircle.diameter should throw an error.


Solution

  • when you try to access the variable with a class it look into only

    cls.__dict__
    

    but when you try to access the variable with instance it looks first

    self.__dict__ 
    

    if find then return or if can't find then it also looks in

    cls.__dict__
    

    here cls is the class

    class Test:
        temp_1=10
        temp_2=20
    
        def __init__(self):
            self.test_1=10
            self.test_2=20
    
        @classmethod
        def c_test(cls):
            pass
    
        def t_method(self):
            pass
    
    
    print Test.__dict__
    print Test().__dict__
    

    Output:

    {'c_test': <classmethod object at 0x7fede8f35a60>, '__module__': '__main__', 't_method': <function t_method at 0x7fede8f336e0>, 'temp_1': 10, '__doc__': None, '__init__': <function __init__ at 0x7fede8f335f0>, 'temp_2': 20}
    
    {'test_2': 20, 'test_1': 10}
    

    For detail class special attribute