繁体   English   中英

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

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

如何在特定的基类上调用方法? 我知道我可以在以下示例中使用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'

只需命名“基类”即可。

如果您想从C类中说B.test

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

例:

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()

输出:

$ python -i foo.py
B
>>>

请参阅: Python类(教程)

暂无
暂无

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

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