Python线性继承,中间类被super()属性调用上的子类覆盖
我的课程设置如下:Python线性继承,中间类被super()属性调用上的子类覆盖,python,python-3.x,inheritance,properties,Python,Python 3.x,Inheritance,Properties,我的课程设置如下: class A(object): __configuration = {} def __init__(self, configuration: dict = {}): A.__configuration = configuration @property def configuration(self): return A.__configuration class B(A): def __init__(self,
class A(object):
__configuration = {}
def __init__(self, configuration: dict = {}):
A.__configuration = configuration
@property
def configuration(self):
return A.__configuration
class B(A):
def __init__(self, configuration: dict = {}):
super().__init__(configuration=configuration)
@property
def configuration(self):
return super().configuration.get('mammals')
class C(B):
def __init__(self, configuration: dict = {}):
super().__init__(configuration=configuration)
@property
def configuration(self):
return super().configuration.get('human')
现在,我遇到了一件我无法回避的事情
当我实例化类C时,它将调用类B的超级init。然后,类B反过来调用类a的超级init
配置将被设置为类a中的类变量,调用property方法时将返回该配置/dict/class变量
类别B我试图让configuration属性获取超级(类别A)配置,然后从中检索“哺乳动物”值
C类然后我试图(当从C类调用时)从它的父类获取“人”值,这将调用B类的属性,我希望B类的属性反过来调用A类的属性。。。从而导致键/值查找:
下面是我在实例化C类时将传入的配置示例:
config = {
'mammals': {
'human': {
...
}
...
}
...
}
exampleInstantiation = C(configuration=config)
我一直在尝试查找线性继承,类方法解析的顺序,但是我找不到任何东西可以让我了解为什么中间类被子类覆盖
非常感谢您的帮助。我不太清楚您的意思。你说的“哺乳动物词典”是什么意思?这个代码没有意义
A
有一个类级\u配置
而不是实例级,并且所有默认值都有可变默认错误。您所说的可变默认错误是什么意思?但是@juanpa.arrivillaga,类B引用了它的父(类A)配置的属性。我将更新我的注释,以给出传入的配置的示例。print(self.configuration)
将打印self.configuration
,而不是self.this class的-definition-of-configuration
。如果self
是C
实例,它将使用C
定义,甚至在B.\uuuu init\uuu
中。它尊重覆盖分辨率,这是您似乎没有预料到的。