2012-03-16 4 views
4

가능한 중복 :
What is the difference between @staticmethod and @classmethod in Python?파이썬 : 클래스 메서드 대 정적 방법의 차이

  • 내가 파이썬에서 OOP를 학습하고 이러한 두 가지 방법에 대해 알게하고
  • 그것을 구문의 차이점은 클래스 메서드가 암시 적으로 첫 번째 매개 변수로 속한 클래스에 전달된다는 것입니다.
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 

6,나는 우리가 클래스를 하드 코딩하지 않기 때문에보다 유연한으로 클래스 메서드를 참조하십시오

질문 :
- 하나 더 그것도 질문인가? @staticmethod 또는 @classmethod?
-이 방법들 각각을 사용하기에 적합한 시나리오는 무엇입니까?

+1

dup http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python – Sid

+1

질문하는 것이 좋습니다. 어떤 것이 더 낫지 만, 어느 것이 적절합니까? 당신이있는 특정한 상황을 위해. – alexis

답변

관련 문제