Calling __init__ with multiple inheritance

jfj jfj at freemail.gr
Tue Mar 29 21:38:58 CEST 2005


Peter Otten wrote:
> 
>>>>Child()
> 
> child
> father
> mother
> parent # <-- parent only once
> <__main__.Child object at 0x402ad38c>
> 

D-uh?

################################################
class Parent(object):
     def __init__(self):
             print "parent"
             super(Parent, self).__init__()

class Father(Parent):
     def __init__(self):
             print "father"
             super(Father, self).__init__()
             print "D-uh"

class Mother(Parent):
     def __init__(self):
             print "mother"
             super(Mother, self).__init__()
             print "D-oh"

class Child(Father, Mother):
     def __init__(self):
             print "child"
             super(Child, self).__init__()

Child()
################################################

This prints
  child
  father
  mother
  parent
  D-oh
  D-uh

Therefore super is a very intelligent function indeed!


jf



More information about the Python-list mailing list