Possible duplicate:
What is the difference between @staticmethod and @classmethod in Python?
- I am learning OOP in python and found out about these two methods.
- It seems that the difference in syntax is that class methods are implicitly passed to the class to which they belong as their first parameter
class Circle: all_circles = [] # class variable @staticmethod def total_area(): for c in Circle.all_circles: # hardcode class name # do somethig @classmethod def total_area(cls): for c in cls.all_circles: # no hardcode class name # do something
I see the class method more flexible since we do not code the class
Question:
βIs that even a better question?β @staticmethod or @classmethod?
- What are the scenarios suitable for using each of these methods?
source share