Python: difference between static methods and class

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?

+4
source share
1 answer

The class method is passed to the cls class that was called. See: What is the difference between @staticmethod and @classmethod in Python?

+4
source

Source: https://habr.com/ru/post/1401903/


All Articles