Search code examples
pythonoopclass-variables

Can I access a class variable from an instance?


I have this class:

class ReallyLongClassName:
    
    static_var = 5

    def instance_method(self):
        ReallyLongClassName.static_var += 1

Is there some way to access the static variable using the self variable?

I'd rather do something like class(self).static_var += 1, because long names are unreadable.


Solution

  • Use self.__class__.classAttr. This should work for both old & new style classes.