3

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'
3
  • I'm assuming that you want to know how to do this for the C class? Do you want to differentiate at runtime, on an instance? Can you show how you want to use this functionality? Commented May 23, 2014 at 0:55
  • @SethMMorton: Well the others don't have any base classes to call Commented May 23, 2014 at 0:56
  • Yes, I realize that. I was prompting for more details on what you want. Commented May 23, 2014 at 0:56

1 Answer 1

7

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.