ancestor class' __init__ doesn't call other methods

Discussion in 'Python' started by Luis P. Mendes, Sep 15, 2006.

  1. 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?
     
    Luis P. Mendes, Sep 15, 2006
    #1
    1. Advertising

  2. 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
     
    Rob De Almeida, Sep 15, 2006
    #2
    1. Advertising

  3. 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
     
    Luis P. Mendes, Sep 15, 2006
    #3
  4. 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.
     
    Bruno Desthuilliers, Sep 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Bethard
    Replies:
    2
    Views:
    459
    Steven Bethard
    Feb 16, 2005
  2. Kent Johnson
    Replies:
    7
    Views:
    916
    Jan Niklas Fingerle
    Feb 12, 2006
  3. Stef Mientki
    Replies:
    2
    Views:
    241
    Stef Mientki
    Apr 28, 2009
  4. Demonic Software
    Replies:
    1
    Views:
    116
    Avdi Grimm
    Mar 17, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    329
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page