473,401 Members | 2,068 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,401 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 1622
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.