如何在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'
1 个回答
7
只需要命名“基类”。
如果你想从你的 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 类(教程)