0
class MyClass(Class1, Class2):
    pass

Both parents have a getImage method.

thing = MyClass()
thing.getImage() #I want to call Class1's
thing.getImage() #I want to call Class2's

Which getImage gets called? How do I specify which one to call?

Name McChange
  • 2,750
  • 5
  • 27
  • 46
  • See this question: http://stackoverflow.com/questions/1848474/method-resolution-order-mro-in-new-style-python-classes – Joel Cornett Jan 17 '13 at 01:36

1 Answers1

2

In this case, thing.getImage will call Class1.getImage provided that it exists. If you want to call the other, you can use the longer form:

Class2.getImage(thing)

These things can be inspected via the class's method resolution order (__mro__):

>>> class foo(object): pass
... 
>>> class bar(object): pass
... 
>>> class baz(foo,bar): pass
... 
>>> print baz.__mro__
(<class '__main__.baz'>, <class '__main__.foo'>, <class '__main__.bar'>, <type 'object'>)

This shows that baz is searched for the method first, then foo, then bar and finally object.

Further reading about multiple inheritance

Further reading about mro

mgilson
  • 300,191
  • 65
  • 633
  • 696