9

Possible Duplicate:
What is the difference between @staticmethod and @classmethod in Python?

  • I am learning OOP in python and came to know about these two methods
  • It seems that the difference in terms of syntax is that class methods are implicitly passed the class they belong to 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 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?

asked Mar 16, 2012 at 20:39
2
  • 1
    dup stackoverflow.com/questions/136097/… Commented Mar 16, 2012 at 20:41
  • 1
    The question I'd ask is not which one is better, but which one is appropriate-- for the particular situation you are in. Commented Mar 16, 2012 at 20:54

1 Answer 1

5

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?

answered Mar 16, 2012 at 20:43
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.