ancestor class' __init__ doesn't call other methods

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Sep 16 00:10:38 CEST 2006


Luis P. Mendes a écrit :
> Hi,
> 
> I have the following problem:
> 
> I instantiate class Sistema from another class.  The result is the same
> if I import it to interactive shell.
> 
> s = Sistema("par")
> 
> class Sistema:
>     def __init__(self, par):
>         cruza_ema = CruzaEmas(par)
> 
> class CruzaEmas(Ema, Cotacoes):
>     def __init__(self, par):
>         Cotacoes.__init__(self, par)  <- calls ancestor class' __init__
> 
> class Cotacoes:
>     def __init__(self, par):
>         print "par: ", par
>         self.a()
>     def a(self):			<- just as an example
>         print "ffffffffffffff"
 >
 > Method a() is not called.  Why is this?

Because there are far too many errors before ?

 > What is the best option to
 > solve this?

Fixing obvious errors would be a good start IMHO.

I tried to run your snippet. Here's a snapshot of the session:

bruno at bibi playground $ cat cotacoes.py
s = Sistema("par")

class Sistema:
     def __init__(self, par):
         cruza_ema = CruzaEmas(par)

class CruzaEmas(Ema, Cotacoes):
     def __init__(self, par):
         Cotacoes.__init__(self, par)  <- calls ancestor class' __init__

class Cotacoes:
     def __init__(self, par):
         print "par: ", par
         self.a()
     def a(self):                         <- just as an example
         print "ffffffffffffff"

bruno at bibi playground $ python cotacoes.py
   File "cotacoes.py", line 9
     Cotacoes.__init__(self, par)  <- calls ancestor class' __init__
                                                   ^
SyntaxError: invalid syntax
bruno at bibi playground $

(fix)

bruno at bibi playground $ python cotacoes.py
   File "cotacoes.py", line 15
     def a(self):                         <- just as an example
                     ^
SyntaxError: invalid syntax

(fix)

bruno at bibi playground $ python cotacoes.py
Traceback (most recent call last):
   File "cotacoes.py", line 1, in ?
     s = Sistema("par")
NameError: name 'Sistema' is not defined

(fix)

bruno at bibi playground $ python cotacoes.py
Traceback (most recent call last):
   File "cotacoes.py", line 5, in ?
     class CruzaEmas(Ema, Cotacoes):
NameError: name 'Ema' is not defined

(fix with a dummy class)

bruno at bibi playground $ python cotacoes.py
Traceback (most recent call last):
   File "cotacoes.py", line 8, in ?
     class CruzaEmas(Ema, Cotacoes):
NameError: name 'Cotacoes' is not defined

(grr... fix)

bruno at bibi playground $ python cotacoes.py
par:  par
ffffffffffffff

here's the fixed code:

bruno at bibi playground $ cat cotacoes.py
class Sistema:
     def __init__(self, par):
         cruza_ema = CruzaEmas(par)

class Ema:
     pass

class Cotacoes:
     def __init__(self, par):
         print "par: ", par
         self.a()
     def a(self):                         # <- just as an example
         print "ffffffffffffff"

class CruzaEmas(Ema, Cotacoes):
     def __init__(self, par):
         Cotacoes.__init__(self, par)  # <- calls ancestor class'

s = Sistema("par")
bruno at bibi playground $

It would have been nice to take the time to test your snippet at least 
once.



More information about the Python-list mailing list