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

change class method to static method #161

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

CunliangGeng
Copy link
Member

@CunliangGeng CunliangGeng commented Jul 11, 2023

The discussion during standup triggered me to reflect on the usage of @classmethod. Only one place in the codebase used @classmethod, which is actually not suitable . So I change it to static method.

👉 When to use @classmethod?
Use @classmethod when the method needs to affect or make use of class-level data (like the count below)

class MyClass:
    count = 0

    def __init__(self):
        MyClass.count += 1

    @classmethod
    def get_count(cls):
        return cls.count

@CunliangGeng CunliangGeng added this to the refactor codebase milestone Jul 11, 2023
Copy link
Contributor

@gcroci2 gcroci2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems more logic this way! I still struggle a bit in understanding clearly when a class method is better than a static one and viceversa.

@CunliangGeng
Copy link
Member Author

Seems more logic this way! I still struggle a bit in understanding clearly when a class method is better than a static one and viceversa.

@classmethod is an advanced feature, which means it's not used in 99.999% situations. Here are some materials to understand it

@CunliangGeng CunliangGeng merged commit 5efcba1 into dev Jul 11, 2023
@CunliangGeng CunliangGeng deleted the change_classmethod_to_staticmethod branch July 11, 2023 11:09
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

Successfully merging this pull request may close these issues.

2 participants