471,616 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

ancestor class' __init__ doesn't call other methods

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
3 1551
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
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
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.

Similar topics

4 posts views Thread by Edvard Majakari | last post: by
50 posts views Thread by Dan Perl | last post: by
6 posts views Thread by Brian Jones | last post: by
2 posts views Thread by Andrea Gavana | last post: by
10 posts views Thread by David Hirschfield | last post: by
reply views Thread by emin.shopper | last post: by
21 posts views Thread by John Henry | last post: by
1 post views Thread by Kevin Walzer | last post: by
21 posts views Thread by Mr.SpOOn | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.