[Tutor] which gets called

Brian van den Broek brian.van.den.broek at gmail.com
Fri Apr 6 16:44:20 CEST 2012


On 6 April 2012 15:54, John Fabiani <johnf at jfcomputer.com> 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


More information about the Tutor mailing list