Re: [Tutor] which gets called
On Friday, April 06, 2012 06:54:28 AM John Fabiani wrote: > 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() > > > Second to insure the right one is called is it possible to do the following > > NewClass(object): > > def onKeyDown(self, event): > b.onKeyDown(event) > > Johnf Thanks guys! The class I'm creating is inheriting from classes I did not create. And of course the inherited classes are from different authors. So I'm attempting to create a wrapper and the problem comes from the keyboard events. Each of the classes has a onKeyDown method and I only want one to work and then pass the data to the second. But you have helped (along with the links). And I have successfully got the right method called. The issue is now getting the second (B) to fire correctly. Johnf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which gets called
On 06/04/2012 14:54, John Fabiani wrote: 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() Please see http://docs.python.org/tutorial/classes.html#multiple-inheritance. This references http://www.python.org/download/releases/2.3/mro/ Having read these why not try typing code into the interactive prompt and see what happens? Worst case you get an exception, if you don't understand it cut and paste it to a reply to this and we'll help out. Second to insure the right one is called is it possible to do the following NewClass(object): def onKeyDown(self, event): b.onKeyDown(event) It's B.onKeyDown(self, event), without the self you'll get an unbound method error. Johnf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which gets called
On 6 April 2012 15:54, John Fabiani wrote: > 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() Hi John, Easy enough to sort out with a little experiment: >>> class A(object): def doit(self): print "A" >>> class B(object): def doit(self): print "B" >>> class C(A,B): def __init__(self): self.doit() >>> c=C() A > Second to insure the right one is called is it possible to do the following > > NewClass(object): > > def onKeyDown(self, event): > b.onKeyDown(event) > Perhaps this helps, some: >>> class D(A,B): def __init__(self): self.doit() def doit(self): print "D" super(D, self).doit() >>> d=D() D A >>> class E(A,B): def __init__(self): B.doit(self) >>> e=E() B >>> Best, Brian vdB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which gets called
John Fabiani wrote: 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() This depends on whether classes A and B are designed for cooperative multiple inheritance or not. The short answer is, A.onKeyDown will be called, because A is listed first. The longer answer is, if A.onKeyDown uses super() to manager multiple inheritance, both A and B.onKeyDown may be called. Here is an example with no cooperative multiple inheritance: class A(object): def onKeyDown(self): print('A deals with keydown event') class B(object): def onKeyDown(self): print('B deals with keydown event') class NewClass(A, B): pass And in use, you will see that A blocks B: py> instance = NewClass() py> instance.onKeyDown() A deals with keydown event And here is a second example using super() for cooperative multiple inheritance: class A(object): def onKeyDown(self): print('A deals with keydown event') super(A, self).onKeyDown() # in Python 3, you can just use "super().onKeyDown()" class B(object): def onKeyDown(self): print('B deals with keydown event') # B does not call super(), because there are no # further parent classes to call. class NewClass(A, B): pass And in use: py> instance = NewClass() py> instance.onKeyDown() A deals with keydown event B deals with keydown event Second to insure the right one is called is it possible to do the following NewClass(object): def onKeyDown(self, event): b.onKeyDown(event) Yes, but that normally should not be necessary if you design your classes carefully. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which gets called
> 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] which gets called
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() Second to insure the right one is called is it possible to do the following NewClass(object): def onKeyDown(self, event): b.onKeyDown(event) Johnf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor