Python 动态继承和属性



我正在寻找一种解决方案,允许我根据满足的某些条件动态继承类(Python 3.6(。看起来很简单,但我无法在子类中提供父类的属性。依赖于self的所有内容要么产生缺少参数错误,要么属性不显示。我实现了此处和此处给出的动态继承问题的解决方案,但仍然遇到子类属性的相同问题。

对于示例:

class Parent:
    def __init__(self):
        self.some_value = 1
    def some_function(self):
        return self.some_value
def classFactory(parent):
    class child(parent):
        def __init__(self, parent):
            super(child, self).__init__()
            parent.__init__(self)
            self.some_other_value = 2
        def some_other_function(self):
            return self.some_value + self.some_other_value
    return child
child_class = classFactory(Parent)
child_class.some_value
AttributeError: type object 'child' has no attribute 'some_value'
child_class.some_other_value
AttributeError: type object 'child' has no attribute 'some_other_value'
child_class.some_other_function()
TypeError: some_other_function() missing 1 required positional argument: 'self'

但是,如果我采用相同的child结构并将其从函数定义中删除,它就可以工作。

class child(Parent):
    def __init__(self, parent):
        super(child, self).__init__()
        parent.__init__(self)
        self.some_other_value = 2
    def some_other_function(self):
        return self.some_value + self.some_other_value
child_class = child(Parent)
print(child_class.some_value)
# 1
print(child_class.some_other_value)
# 2
print(child_class.some_other_function())
# 3

为什么在第一种情况下没有继承属性,但在第二种情况下却继承了属性?如何编写动态继承以提供我期望的行为(如第二种情况所示(?

如果我在 return child(parent) 中使用父参数实例化子类,它可以工作。这将保留父项和子项的属性和方法。

class Parent:
    def __init__(self):
        self.some_value = 1
    def some_function(self):
        return self.some_value
def classFactory(parent):
    class child(parent):
        def __init__(self, parent):
            parent.__init__(self)
            self.some_other_value = 2
        def some_other_function(self):
            return self.some_value + self.some_other_value
    return child(parent)
child_class = classFactory(Parent)
print(child_class.some_value)
# 1
print(child_class.some_other_value)
# 2    
print(child_class.some_other_function())
# 3
print(child_class.some_function())
# 1

动态继承的最显式实现可以使用元类实现:

class A:pass
class B:pass

class Meta(type):
     def __new__(cls, name, bases, dct):
         bases = (A,) if Meta.condition() else (B,)
         return type(name, bases, dct)
     @staticmethod
     def condition():
         # implement condition.
         ...
class C(metaclass=Meta):
    pass
c_instance = C()
print("is C instance of A:")
print(isinstance(c_instance, A))
print("is C instance of B:")
print(isinstance(c_instance, B))

或者,您可以定义条件函数及其,如下所示:


condition = lambda: True
class D(A if condition() else B):
    pass
d_instance = D()
print("is D instance of A:")
print(isinstance(d_instance, A))
print("is C instance of B:")
print(isinstance(d_instance, B))

元类的方法将使您能够更好地控制类的创建,因此这取决于您的需求......

最新更新