Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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 - Fatal编程技术网

Python super在多级继承中的应用

Python super在多级继承中的应用,python,python-3.x,Python,Python 3.x,我有一个代码如下: class A(): def something(): #Do something class B(A): def something(): super(B, self).something() #Do something class C(B): def something(): if VeryRareSpecificCase: super(super(C,se

我有一个代码如下:

class A():
    def something():
        #Do something

class B(A):
    def something():
        super(B, self).something()
        #Do something

class C(B):
    def something():
        if VeryRareSpecificCase:
            super(super(C,self)).something()
        else:
            super(C, self).something()
        #Do something

但是,它会引发错误TypeError:必须是type,而不是super。

如果您没有使用复杂的菱形继承,您可以使用以下方法跳过链中的父级:

class C(B):
    def something(self):
        if VeryRareSpecificCase:
            super(B, self).something()  # <-- note: B passed explicitly!
        else:
            super().something()
C类(B类):
定义某物(自我):
如果是特定情况:

super(B,self).something()#B-->A-->对象
,如MCVE中所示,这应该是安全的。然而,若您有比这更复杂的事情,那个么您必须直接使用,并精确地指定您需要手动执行的行为

你希望super(super(C,self))是指什么?如果答案是
super(B,self)
,那么您没有正确使用
super
;您应该只对类引用进行硬编码。在继承之前,我正在从另一个模块导入B,而A位于我不想访问的模块中。这会影响什么吗?或者我还能这样做吗?