satwikkansal / wtfpython

What the f*ck Python? 😱
Do What The F*ck You Want To Public License
35.7k stars 2.65k forks source link

Add notes for modifying class attributes #303

Open ZeroRin opened 1 year ago

ZeroRin commented 1 year ago

Relates to class attributes and instance attributes considering the following code:

class Foo:
    _count1 = 0
    _count2 = 0
    @staticmethod
    def total():Foo._count2 += 1
    @classmethod
    def count(cls):cls._count1 += 1
    def __init__(self) -> None:
        self.total()
        self.count()

class Foo1(Foo): pass
class Foo2(Foo): pass

a,b,c = Foo1(),Foo1(),Foo2()
print(Foo._count1, Foo._count2, Foo1._count1, Foo1._count2, Foo2._count1, Foo2._count2)
# 0 3 2 3 1 3

The @classmethod way counts instance for each class separately, while the @staticmethod way counts for all subclasses together. While this is easy to understand with the knowledge of how attributes works, users may not realize this until they run into the problem. Thus, I think it's worth noting so that users can learn to choose the correct way of setting attributes according to their actual needs.

ZeroRin commented 1 year ago

Just noticed that running Foo() before any subclass instance is created would affect counter for all subclasses. Should add def __init_subclass__(cls): cls._count1 = 0 to the base class

satwikkansal commented 1 year ago

Thanks, yes, I agree that's an interesting thing to add to the collection :)