Casebash
Casebash

Reputation: 118802

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?

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'

Upvotes: 3

Views: 1734

Answers (1)

James Mills
James Mills

Reputation: 19030

Just name the "base class".

If you wanted to call say B.test from your C class:

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)

Upvotes: 7

Related Questions