473,241 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 1618
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edvard Majakari | last post by:
Hi, I was wondering what would be the most elegant way for creating a Python class wrapper for a command line utility, which takes three types of arguments: 1. options with values (--foo=bar)...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
2
by: Andrea Gavana | last post by:
Hello NG, this may seem a stupid (or even impossible) question, but my knowlegde of Python is quite limited. I have basically a simple graphical user interface that contains a Panel, another...
10
by: David Hirschfield | last post by:
Here's a strange concept that I don't really know how to implement, but I suspect can be implemented via descriptors or metaclasses somehow: I want a class that, when instantiated, only defines...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
21
by: John Henry | last post by:
Hi list, I have a need to create class methods on the fly. For example, if I do: class Dummy: def __init__(self): exec '''def method_dynamic(self):\n\treturn self.method_static("it's...
1
by: Kevin Walzer | last post by:
I'm trying to create a custom Tkinter widget class, and I'm having some difficulty getting it set up properly. The class is called MacToolbar, saved in its own module MacToolbar.py, and imported...
21
by: Mr.SpOOn | last post by:
Hi, I'm going to work on a project to represent some musical theory in Python, in an object oriented way. I have to manage many elements of music such as notes, intervals, scales, chords and so...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.