Python : Difference between static methods vs class method
Possible Duplicate:
What is the difference between @staticmethod and @classmethod in Python?
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 class method as more flexible since we don't hardcode the class
Question:
- Is it even a question which one is better? @staticmethod or @classmethod?
- what are the scenarios suitable to use of each one of these methods?
A classmethod gets passed the class 'cls' that it was called upon. For more details see: What is the difference between @staticmethod and @classmethod in Python?
链接地址: http://www.djcxy.com/p/9172.html下一篇: Python:静态方法和类方法的区别