Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 使用父类B中父类A的方法_Python_Python 3.x_Inheritance_Multiple Inheritance_Superclass - Fatal编程技术网

Python 使用父类B中父类A的方法

Python 使用父类B中父类A的方法,python,python-3.x,inheritance,multiple-inheritance,superclass,Python,Python 3.x,Inheritance,Multiple Inheritance,Superclass,我有一个甲级: class A(object): def pprint(x): print(x) 然后我有一个B班: class B(object): def pprint(x): x += 1 # find a way to call A.pprint(x) 然后我有一个儿童班: class Child(B, A): pass 应使用哪种方法: child = Child() child.pprint(1) >&g

我有一个甲级:

class A(object):
   def pprint(x):
       print(x)
然后我有一个B班:

class B(object):
    def pprint(x):
        x += 1
        # find a way to call A.pprint(x)
然后我有一个儿童班:

class Child(B, A):
    pass
应使用哪种方法:

child = Child()
child.pprint(1)
>>> 2

我可以对B进行更改,但不能对A进行更改。我不能在B中直接引用A。B永远不会直接实例化,总是通过子类进行实例化。

您有几个选项可以从B类访问A方法,而不必从A继承B

首先,您可以创建一个staticmethod并从B调用它

class A(object):
   @staticmethod
   def pprint(x):
       print(x)

class B(object):
    def pprint(self, x):
        print(x + 1)
        A.pprint(x)
或者你可以像这样在B中继承A:

class A(object):
    def pprint(self, x):
        print(x)

class B(A):
    def pprint(self, x):
        print(x + 1)
        super(B, self).pprint(x)
那么对于您的子类,仅从B继承:

class Child(B):
    pass


>>> c = Child()
>>> c.pprint(1)
2
1
好的,最新的解决方案

import inspect

class C(B, A):
    def pprint(self, x):
        a_class = inspect.getmro(Child)[-2]
        a_class.pprint(self, x)

由于object将是inspect.getmroChild中的最后一个结果,因此我们跳过该结果以获取最后一个结果之前的结果,即A。然后我们调用该类的pprint方法。更确切地说,如果您知道要调用的类的名称,您还可以迭代inspect.getmroChild中的结果并找到所需的结果。

您有几个选项可以从B类访问a方法,而不必从a继承B

首先,您可以创建一个staticmethod并从B调用它

class A(object):
   @staticmethod
   def pprint(x):
       print(x)

class B(object):
    def pprint(self, x):
        print(x + 1)
        A.pprint(x)
或者你可以像这样在B中继承A:

class A(object):
    def pprint(self, x):
        print(x)

class B(A):
    def pprint(self, x):
        print(x + 1)
        super(B, self).pprint(x)
那么对于您的子类,仅从B继承:

class Child(B):
    pass


>>> c = Child()
>>> c.pprint(1)
2
1
好的,最新的解决方案

import inspect

class C(B, A):
    def pprint(self, x):
        a_class = inspect.getmro(Child)[-2]
        a_class.pprint(self, x)

由于object将是inspect.getmroChild中的最后一个结果,因此我们跳过该结果以获取最后一个结果之前的结果,即A。然后我们调用该类的pprint方法。更确切地说,如果您知道要调用的类的名称,您还可以迭代inspect.getmroChild中的结果,找到您想要的类。

在解释之后-您需要的不是super,您需要类似sibling\u super的东西来查找多重继承链中的下一个类。您可以为此轮询Python的MRO,例如:

class A(object):

    def pprint(self, x):  # just to make it valid, assuming it is valid in the real code
        print(x)

class B(object):

    @staticmethod
    def sibling_super(cls, instance):
        mro = instance.__class__.mro()
        return mro[mro.index(cls) + 1]

    def pprint(self, x):
        x += 1
        self.sibling_super(B, self).pprint(self, x)

class Child(B, A):
    pass

child = Child()
child.pprint(1)  # 2

在解释之后-您需要的不是super,您需要类似sibling_super的东西来查找多重继承链中的下一个类。您可以为此轮询Python的MRO,例如:

class A(object):

    def pprint(self, x):  # just to make it valid, assuming it is valid in the real code
        print(x)

class B(object):

    @staticmethod
    def sibling_super(cls, instance):
        mro = instance.__class__.mro()
        return mro[mro.index(cls) + 1]

    def pprint(self, x):
        x += 1
        self.sibling_super(B, self).pprint(self, x)

class Child(B, A):
    pass

child = Child()
child.pprint(1)  # 2

这两种选择在我的情况下都不起作用。我简化了这个问题,使它更具表现力,但在实际情况中,我有许多其他类,如类A,都使用相同的方法。因此,您可以有ChildB,A,但也可以有另一个ChildB,D或YetAnotherChildB,E。出于同样的原因,您提供的第二个选项也是不可行的。@EdgardDerby-所以您不知道在多重继承中使用的第二个类名是什么?正确。我只知道,在上面的示例中,将与类B共同作为父对象的类有一个已知接口,所有类都绑定有一个方法pprint。这就是为什么我不能在B类中直接引用A类。@EdgardDerby-在这种情况下探索MRO。您可能需要添加一些防护,例如,MRO中是否真的有您的类。检查上面的更新。这两个选项在我的情况下都不起作用。我简化了这个问题,使它更具表现力,但在实际情况中,我有许多其他类,如类A,都使用相同的方法。因此,您可以有ChildB,A,但也可以有另一个ChildB,D或YetAnotherChildB,E。出于同样的原因,您提供的第二个选项也是不可行的。@EdgardDerby-所以您不知道在多重继承中使用的第二个类名是什么?正确。我只知道,在上面的示例中,将与类B共同作为父对象的类有一个已知接口,所有类都绑定有一个方法pprint。这就是为什么我不能在B类中直接引用A类。@EdgardDerby-在这种情况下探索MRO。您可能需要添加一些防护,例如,MRO中是否真的有您的类。检查上面的更新。正如我在下面提到的,两个选项都不起作用。在帖子中,我设置了两个约束条件:a我不能修改a类,b我无论如何都不能直接引用b类中的a类。我只能使用super、Class、base等@EdgardDerby我不知道为什么我提供的第二个选项不起作用,因为你没有直接修改a。你根本没有修改它。你做的正是你刚才说的你想做的。从B调用超级函数。我提到了两个约束:第二个约束是,你不能以任何方式引用B类中的A类,这包括让B直接从A继承。我在@zwer的回答下面提供了关于具体情况的更多细节。啊,我明白了。对不起,我没有考虑直接引用类的继承。@EdgardDerby您能修改Child吗?如果是这样的话,我已经提供了另一个解决方案,您可以从child访问A的pprint方法,正如我在下面提到的,这两个选项都不起作用。在帖子中,我设置了两个约束条件:a我不能修改a类,b我无论如何都不能直接引用b类中的a类。我只能使用super、Class、base等@EdgardDerby我不知道为什么我提供的第二个选项不起作用,因为你没有直接修改a。你根本没有修改它。你做的正是你刚才说的你想做的。打电话给
B的超级函数。我提到了两个约束:第二个约束是,你不能以任何方式引用B类中的A类,这包括让B直接从A继承。我在@zwer的回答下面提供了关于具体情况的更多细节。啊,我明白了。对不起,我没有考虑直接引用类的继承。@EdgardDerby您能修改Child吗?如果是这样,我提供了另一个解决方案,您可以从Child访问A的pprint方法