Python 筛选对父方法的调用

Python 筛选对父方法的调用,python,class,oop,inheritance,Python,Class,Oop,Inheritance,我使用组合而不是继承来扩展父类。我将方法调用从我的“子项”传递到父项,如代码所示: class Child(object): def __init__(self, obj): self.obj = obj def __getattr__(self, attr): """Pass properties and method calls to Parent class""" result = getattr(self.obj, att

我使用组合而不是继承来扩展父类。我将方法调用从我的
“子项”
传递到
父项
,如代码所示:

class Child(object):
    def __init__(self, obj):
        self.obj = obj

    def __getattr__(self, attr):
        """Pass properties and method calls to Parent class"""
        result = getattr(self.obj, attr)
        return result
现在一些方法调用(例如
Parent.method()
)返回一个
Parent
实例。在这些情况下,我想要的是能够返回一个
实例。我在想这样的事情可以做到:

    def __getattr__(self, attr):
        """Pass properties and method calls to Parent class"""
        result = getattr(self.obj, attr)
        return Child(result)

但如果失败并出现:TypeError:“Child”对象不可调用。我还看到,
result
具有类型
instancemethod
,而不是调用该方法的实际结果(这将是一个
Parent
实例)。实现这一点的正确方法是什么?

看起来您可能会重写变量Child,因为子实例应该是可调用的。 试着找一些像

Child = Child(some_argument)