Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 394
Casebash Avatar asked Dec 17 '25 11:12

Casebash


1 Answers

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)

like image 84
James Mills Avatar answered Dec 19 '25 07:12

James Mills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!