Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python线性继承,中间类被super()属性调用上的子类覆盖_Python_Python 3.x_Inheritance_Properties - Fatal编程技术网

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类的属性。。。从而导致键/值查找:

  • 获取整个配置
  • 获取哺乳动物值(这是另一个config/dict)
  • 获取人的价值(类型不重要)
  • 问题: 当我实例化类C时,它会在实例化时传播到类A。A类的配置设置正确。然后,在类B中,当我引用super()配置时,它最终返回我的子(类C)的配置

    例如,如果我将这一行放在类B的init中,并让实例化从实例化类C执行:

    打印(自我配置) 它将返回给我的是人类词典的值,而不是我想要的哺乳动物词典的值


    下面是我在实例化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
    中。它尊重覆盖分辨率,这是您似乎没有预料到的。