[Tutor] which gets called

Evert Rol evert.rol at gmail.com
Fri Apr 6 16:33:21 CEST 2012


> Hi,
> 
> I want to create a class that inherits two other classes.
> 
> class NewClass( A,B)
> 
> But both "A" and "B" contain a method with the same name ("onKeyDown"). 
> 
> If my "NewClass" does not contain something to override the methods which one 
> would be called if
> 
> myinstance = NewClass()
> 
> myinstance.onKeyDown()
> 

If I remember correctly, A.onKeyDown.
But things can get more complicated in other cases. See also the following post for a read on the MRO (method resolution order); could help to clarify things (or confuse you further): http://python-history.blogspot.com/2010/06/method-resolution-order.html


> Second to insure the right one is called is it possible to do the following
> 
> NewClass(object):
> 
>  def onKeyDown(self, event):
>      b.onKeyDown(event)

What is b here? Are you (trying to) save(ing) a parent as a instance in the class? Or should that be uppercase B?
The latter would work, I think, though you'll have to put `self` here explicitly (since you're calling it without an instance, *and* you want to tell the method the instance is NewClass() instead of eg B()):

  def onKeyDown(self, event):
      B.onKeyDown(self, event)


Cheers,

  Evert



More information about the Tutor mailing list