wtfpython icon indicating copy to clipboard operation
wtfpython copied to clipboard

Add notes for modifying class attributes

Open ZeroRin opened this issue 3 years ago • 2 comments

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 avatar Dec 31 '22 03:12 ZeroRin

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

ZeroRin avatar Dec 31 '22 04:12 ZeroRin

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

satwikkansal avatar Jan 05 '23 14:01 satwikkansal