By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,724 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,724 IT Pros & Developers. It's quick & easy.

ancestor class' __init__ doesn't call other methods

P: n/a
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?
Sep 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Luis P. Mendes wrote:
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

Sep 15 '06 #2

P: n/a
Rob De Almeida escreveu:
Luis P. Mendes wrote:
>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:
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
Sep 15 '06 #3

P: n/a
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.
Sep 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.