Open ZeroRin opened 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
Thanks, yes, I agree that's an interesting thing to add to the collection :)
Relates to class attributes and instance attributes considering the following code:
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.