TP schrieb:
Hi,
Hereafter is an example using super.
At the execution, we obtain:
coucou
init_coucou2
coucou1
coucou2
Traceback (most recent call last):
File "essai_heritage.py", line 34, in <module>
print b.a
AttributeError: 'coucou' object has no attribute 'a'
Why Python does not enter in the __init__ method of coucou1?
Because you use super wrong. It's not supposed to be called with a
superclass, but with the current class. And each class needs to call
super itself in it's own __init__-method.
Like this:
class coucou1( object ):
def __init__( self
, a = 1 ):
self.a = a
print "init_coucou1"
super( coucou1, self ).__init__( )
def print_coucou1( self ):
print "coucou1"
class coucou2( object ):
def __init__( self
, b = 2 ):
self.b = b
print "init_coucou2"
super( coucou2, self ).__init__( )
def print_coucou2( self ):
print "coucou2"
class coucou( coucou1, coucou2 ):
def __init__( self ):
print "coucou"
super( coucou, self ).__init__( )
b = coucou()
b.print_coucou1()
b.print_coucou2()
print b.a
print b.b
Diez
--
http://mail.python.org/mailman/listinfo/python-list