2015-05-22 118 views
1

我目前正在研究嵌入Python以正確設置問題的C代碼。因此,用戶應該從定義一般結構的Python類派生自己的問題。作爲最小的例子,我使用Python:檢測是否從C API重新定義/重載了類方法

class testbase(object): 
    def __init__(self,name): 
     self._name = name 
    def method1(self): 
     pass 
    def method2(self): 
     pass 

作爲基類。現在,每一個問題應該從這個類中派生自己的問題一樣

class test(testbase): 
    def __init__(self,name): 
     self._name = name 
    def method1(self): 
     print self._name 

其中method2不是由用戶自定義。現在我想從C API或至少在Python中檢測派生類是否提供method2或者是否從基類中使用它。

的第一答案並不那麼準確,因爲我也這樣想,如果有測試類

T = test('test') 

我想知道,如果T.method2test定義或testbase繼承的一個實例。

+0

的可能重複的[Python中:確定是否方法重寫](http://stackoverflow.com/questions/ 2319107/python -definition-if-method-was-overridden) – satoru

回答

2

感謝@ZachGates,我用自己的想法來擴展它的類和對象的工作:

class Parent(object): 
    def __init__(self): 
     pass 

    def parent_method(self): 
     print('parent') 

    @classmethod 
    def parent_class_method(self): 
     print('parent class method') 

class Child(Parent): 
    def __init__(self): 
     pass 

    def child_method(self): 
     print('child') 

    @classmethod 
    def child_class_method(self): 
     print('child class method') 

def is_inherited_instance_method(test_object, method_name): 
    """ 
    Gets the super object and looks up for it's method. 
    If the child has `method_name` atribute and parent doesn't, 
    then method is new. 
    """ 

    super_object = super(test_object.__class__, test_object) 
    if hasattr(test_object, method_name) and not hasattr(super_object, method_name): 
     # You can add extra checks, such as: callable() ... 
     print('it is a new method: %s' % method_name) 
    else: 
     print('method is not new: %s' % method_name) 

def is_inherited_class_method(test_cls, method_name): 
    """ Does the same but only with Classes. """ 

    super_cls = super(test_cls, test_cls) 
    if hasattr(test_cls, method_name) and not hasattr(super_cls, method_name): 
     # You can add extra checks, such as: callable() ... 
     print('it is a new class method: %s' % method_name) 
    else: 
     print('class method is not new: %s' % method_name) 


parent = Parent() 
child = Child() 

is_inherited_instance_method(parent, 'parent_method') 
is_inherited_instance_method(child, 'parent_method') 
is_inherited_instance_method(child, 'child_method') 

is_inherited_class_method(Parent, 'parent_class_method') 
is_inherited_class_method(Child, 'parent_class_method') 
is_inherited_class_method(Child, 'child_class_method') 

並且輸出是:

it is a new method: parent_method 
method is not new: parent_method 
it is a new method: child_method 

it is a new class method: parent_class_method 
class method is not new: parent_class_method 
it is a new class method: child_class_method 
1

指定子類時,超類中的所有方法都將被繼承。

class A(object): 
    def foo(self): 
     pass 

class B(A): 
    def bar(self): 
     pass 

A有一個方法:foo。雖然類B有兩種方法:創建的bar方法和繼承的foo方法。

您可以檢查的方法是繼承這樣的:

>>> def inherited(cls, method): 
...  if hasattr(cls, method) and hasattr(super(cls, cls), method): 
...   return A.foo == B.foo 
...  else: 
...   return False 
... 
>>> inherited(B, 'foo') 
True 
>>> inherited(B, 'bar') 
False 
+0

我想檢查該方法是否是繼承的方法。 –

+0

編輯添加。 @Grisu –

+0

仍然在類上工作,而不是在類的實例上。 –

相關問題