Say I have a (normal bound) method in my class that has to access class variables (like a counter in __init__). If I want to modify class varaibles, I see three possibilities:
- Use type(self)orself.__class__(after reading this, I chosetypeover__class__, as I'm using Python 3 so "old-style" classes don't apply)
- Use the name of the class. This works well unless the name is rebound.
- write a @classmethodspecifically for changing class variables. This is the most complicated, but also the most clear (IMO) approach.
The three methods would be written like this:
class MyClass:
    counter = 0
    def f1(self):
        type(self).counter += 1
    def f2(self):
        MyClass.counter += 1
    def f3(self):
        self._count()
    @classmethod
    def _count(cls):
        cls.counter += 1
Is there a clear "best" option and which one is it or are they all more or less equivalent and it's just "what I like most"? Does inheritance change anything?
 
    