Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notes for modifying class attributes #303

Open
ZeroRin opened this issue Dec 31, 2022 · 2 comments
Open

Add notes for modifying class attributes #303

ZeroRin opened this issue Dec 31, 2022 · 2 comments

Comments

@ZeroRin
Copy link

ZeroRin commented Dec 31, 2022

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
Copy link
Author

ZeroRin commented Dec 31, 2022

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants