2013-02-05 45 views
2

如何在Python父類中指定需要在子類中重寫某些字段/方法?強制覆蓋字段/方法?

+0

可能的重複http://stackoverflow.com/questions/1151212/equivalent-of-notimplementederror-for-fields-in-python – LSerni

回答

4

你可以提出一個NotImplementedError

def my_method(self, arg): 
    raise NotImplementedError('Implement me') 

有關屬性,你可以使用@property裝飾:

@property 
def my_property(self): 
    raise NotImplementedError('Implement me as well') 
+0

是的,這應該照顧一種方法,但怎麼樣的領域? –

+0

@AlBundy:看我的編輯。 – Blender

+0

非常感謝! –

1

你可以看看abstract base class模塊。

但是,更簡單的替代方法是定義不執行任何操作的存根實現,或者提出NotImplementedError