2010-06-11 3 views
3

나는 Flask에 CouchDB 지원을 추가하는 Flask 확장을 연구 중이다. 더 쉽게 만들려면 couchdb.mapping.Document을 하위 클래스로 만들었으므로 storeload 메서드는 현재 스레드 로컬 데이터베이스를 사용할 수 있습니다. 현재 코드는 다음과 같습니다.Python에서 수퍼 클래스의 클래스 메소드 호출하기

class Document(mapping.Document): 
    # rest of the methods omitted for brevity 
    @classmethod 
    def load(cls, id, db=None): 
    return mapping.Document.load(cls, db or g.couch, id) 

나는 약간의 설명을 생략했지만, 중요한 부분입니다. 그러나 classmethod이 작동하는 방식에 나는이 메소드를 호출 할 때, 나는

File "flaskext/couchdb.py", line 187, in load 
    return mapping.Document.load(cls, db or g.couch, id) 
TypeError: load() takes exactly 3 arguments (4 given) 

내가 mapping.Document.load.im_func(cls, db or g.couch, id)로 전화를 대체 시험 오류 메시지가 나타납니다, 그것은 작동하지만 난에 액세스하는 방법에 대한 특히 행복하지 않다 인해 내부 im_ 속성 (문서화되어 있음에도 불구하고). 누구든지 이것을 처리하는 더 우아한 방법이 있습니까?

답변

7

여기 실제로 super을 사용해야한다고 생각합니다. 어쨌든 수퍼 클래스 메서드를 호출하는 깔끔한 방법입니다.

class A(object): 
    @classmethod 
    def load(cls): 
     return cls 

class B(A): 
    @classmethod 
    def load(cls): 
     # return A.load() would simply do "A.load()" and thus return a A 
     return super(B, cls).load() # super figures out how to do it right ;-) 


print B.load() 
+0

감사합니다. '슈퍼'작품은 언제나 저에게 다소 불투명합니다. – LeafStorm