简体   繁体   English

如何在Python中的特定基类上调用方法?

[英]How do I call a method on a specific base class in Python?

How do I call a method on a specific base class? 如何在特定的基类上调用方法? I know that I can use super(C, self) in the below example to get automatic method resolution - but I want to be able to specify which base class's method I am calling? 我知道我可以在以下示例中使用super(C, self)来获得自动方法解析-但我希望能够指定我正在调用哪个基类的方法?

class A(object):
    def test(self):
        print 'A'

class B(object):
    def test(self):
        print 'B'

class C(A,B):
    def test(self):
        print 'C'

Just name the "base class". 只需命名“基类”即可。

If you wanted to call say B.test from your C class: 如果您想从C类中说B.test

class C(A,B):
    def test(self):
        B.test(self)

Example: 例:

class A(object):

    def test(self):
        print 'A'


class B(object):

    def test(self):
        print 'B'


class C(A, B):

    def test(self):
        B.test(self)


c = C()
c.test()

Output: 输出:

$ python -i foo.py
B
>>>

See: Python Classes (Tutorial) 请参阅: Python类(教程)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM