You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
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.
The text was updated successfully, but these errors were encountered: