ancestor class' __init__ doesn't call other methods

L

Luis P. Mendes

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? What is the best option to
solve this? Have Cotacoes returning values and not to be an ancestor
class of CruzaEmas?
 
R

Rob De Almeida

Luis said:
Method a() is not called. Why is this? What is the best option to
solve this? Have Cotacoes returning values and not to be an ancestor
class of CruzaEmas?

It works for me, after rearranging your code a little bit:


class Ema:
pass

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

class Cotacoes:
def __init__(self, par):
print "par: ", par
self.a()
def a(self):
print "ffffffffffffff"

class CruzaEmas(Ema, Cotacoes):
def __init__(self, par):
Cotacoes.__init__(self, par)

s = Sistema("par")


--Rob
 
L

Luis P. Mendes

Rob De Almeida escreveu:
It works for me, after rearranging your code a little bit:
Ok, thanks. I already know where the problem was.

The a() method was just an example. The real problem was elsewhere.

I had two methods with the same name, one at Cotacoes (the ancestor),
another one at CruzaEmas.

The method called at the __init__ of the ancestor was the one of the
daughter class, and not the one of the ancestor's, as I was expecting.
After changing the name of the latter, everything seems to be fine.

Luis P. Mendes
 
B

Bruno Desthuilliers

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@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@bibi playground $ python cotacoes.py
File "cotacoes.py", line 9
Cotacoes.__init__(self, par) <- calls ancestor class' __init__
^
SyntaxError: invalid syntax
bruno@bibi playground $

(fix)

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

(fix)

bruno@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@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@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@bibi playground $ python cotacoes.py
par: par
ffffffffffffff

here's the fixed code:

bruno@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@bibi playground $

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top