2013-06-24 1 views
2

내가 다른 클래스가 상속되어야하는 기본 클래스가 있습니다어떻게 wxPython, abc 및 메타 클래스 혼합을 결합합니까?

class AppToolbar(wx.ToolBar): 
    ''' Base class for the Canary toolbars ''' 

    def __init__(self, *args, **kwargs): 
     super().__init__(*args, **kwargs) 

     # ... a few common implementation details that work as expected... 

     self._PopulateToolbar() 
     self.Realize() 

기본 클래스하지 않는 (및 수 없습니다) _PopulateToolbar()을 구현; 그것은 추상적 인 방법이어야합니다. 따라서, 나는 abc이 좋은 계획이었다 사용하여 생각, 그래서이 시도 :이 TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases을 주도 실행하려고하면, 아마도 당연히

class AppToolbar(wx.ToolBar, metaclass=abc.ABCMeta): 
    # ... as above, but with the following added 
    @abc.abstractmethod 
    def _PopulateToolbar(): 
     pass 

합니다.

class PopulateToolbarMixin(metaclass=ABCMeta): 
    @abstractmethod 
    def _PopulateToolbar(self): 
     pass 

PopulateToolbarMixin.register(wx.ToolBar) 
PopulateToolbarMixin.register(AppToolbar) 

변화 없음 : 여전히 같은 TypeError 메시지 나는 "아, 맞다, 난 그냥 믹스 인을 사용합니다", 생각했다. 나는 ABCMeta의 사용으로 명백한 무엇인가를 놓치고 있다고 생각한다. 이 wxPython 특정 오류처럼 보이지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 같은 문제에 접근하는 더 좋은 방법이 있습니까?

편집 : 그것은 하나의 메타 클래스를 혼합 할 수있는 동료와 대화에서 나에게 지적되었다. wx.ToolBar은 분명히 sip.wrappertype에서 파생되기 때문에이를 수행 할 방법이없는 것으로 보입니다. "추상적 방법"접근 방식을 다루는 또 다른 방법은 무엇입니까? 당신이 wx.ToolBar 및 abc.ABCMeta에서 상속 첫 번째 예에서

답변

1

, 당신은 AppToolbar이 abc.ABCMeta의 서브 클래스되고 싶지 않아, 당신은 AppToolbar 그것의인스턴스가되고 싶어요. 이 시도 :

class AppToolbar(wx.ToolBar, metaclass=abc.ABCMeta): 
    # ... as above, but with the following added 
    @abc.abstractmethod 
    def _PopulateToolbar(): 
     pass 

비록 조금 더 가까이이보고, wx.Toolbar는 메타 클래스의 인스턴스 인 당신이, 그 메타 클래스로 abc.ABCMeta와 wx.Toolbar의 서브 클래스를 정의 할 수 없습니다 것으로 보인다 bultins.type 이외. 그러나 AppToolbar에서 추상적 인 동작을 벗어날 수 있습니다.

class AppToolbar(wx.ToolBar): 
    def _PopulateToolbar(): 
     ''' This is an abstract method; subclasses must override it. ''' 

     raise NotImplementedError('Abstract method "_PopulateToolbar" must be overridden before it can be called.') 
+0

음, 잘 잡습니다. 실제로 원래 있던 코드입니다 (코드를 다시 작성할 때 잊어 버렸습니다). 아아, wxPython과의 이상한 충돌이 있습니다. 작동하지 않는 것처럼 보입니다. –

+1

그러면 wx.ToolBar가 타입의 직접적인 인스턴스가 아닌 것처럼 보일 것입니다.이 경우에는 행운을 빕니다. 아는 한, 파이썬에서 메타 클래스를 결합하는 좋은 방법은 없습니다. –

관련 문제