Inheritance problem ?

T

tooper

Hello all,

I'm trying to implement a common behavior for some object that can be
read from a DB or (when out of network) from an XML extract of this DB.
I've then wrote 2 classes, one reading from XML & the other from the
DB, both inheritating from a common one where I want to implement
several common methods.
Doing this, I've come to some behaviour I can't explain to myself,
which I've reproduced in the example bellow :

-----

class myfather:
def __repr__(self):
return "\t a="+self.a+"\n\t b="+self.b

class mychilda(myfather):
def __init__(self,a):
self.a= a
def __getattr__(self,name):
return "Undefined for mychilda"

class mychildb(myfather):
def __init__(self,b):
self.b= b
def __getattr__(self,name):
return "Undefined for mychildb"

a= mychilda("a")
b= mychildb("b")

print "a:\n"+str(a)
print "b:\n"+str(b)

-----

I was expecting to get :

a:
a= a
b= Undefined for mychilda
b:
a= Undefined for mychildb
b= b

but I get the following error :

File "/home/thierry/mytest.py", line 20, in ?
print "a:\n"+str(a)
TypeError: 'str' object is not callable

Could someone explain me what I missed ?

Thanks in advance !
 
D

db

Hello all,

I'm trying to implement a common behavior for some object that can be
read from a DB or (when out of network) from an XML extract of this DB.
I've then wrote 2 classes, one reading from XML & the other from the
DB, both inheritating from a common one where I want to implement
several common methods.
Doing this, I've come to some behaviour I can't explain to myself,
which I've reproduced in the example bellow :

-----

class myfather:
def __repr__(self):
return "\t a="+self.a+"\n\t b="+self.b

class mychilda(myfather):
def __init__(self,a):
self.a= a
def __getattr__(self,name):
return "Undefined for mychilda"

class mychildb(myfather):
def __init__(self,b):
self.b= b
def __getattr__(self,name):
return "Undefined for mychildb"

a= mychilda("a")
b= mychildb("b")

print "a:\n"+str(a)
print "b:\n"+str(b)

-----

I was expecting to get :

a:
a= a
b= Undefined for mychilda
b:
a= Undefined for mychildb
b= b

but I get the following error :

File "/home/thierry/mytest.py", line 20, in ?
print "a:\n"+str(a)
TypeError: 'str' object is not callable

Could someone explain me what I missed ?

Thanks in advance !

try new style classes.
class myfather(object):

see http://users.rcn.com/python/download/Descriptor.htm

HTH Arjen
 
J

jitya

tooper said:
Hello all,

I'm trying to implement a common behavior for some object that can be
read from a DB or (when out of network) from an XML extract of this DB.
I've then wrote 2 classes, one reading from XML & the other from the
DB, both inheritating from a common one where I want to implement
several common methods.
Doing this, I've come to some behaviour I can't explain to myself,
which I've reproduced in the example bellow :

-----

class myfather:
def __repr__(self):
return "\t a="+self.a+"\n\t b="+self.b

class mychilda(myfather):
def __init__(self,a):
self.a= a
def __getattr__(self,name):
return "Undefined for mychilda"

class mychildb(myfather):
def __init__(self,b):
self.b= b
def __getattr__(self,name):
return "Undefined for mychildb"

a= mychilda("a")
b= mychildb("b")

print "a:\n"+str(a)
print "b:\n"+str(b)

-----

I was expecting to get :

a:
a= a
b= Undefined for mychilda
b:
a= Undefined for mychildb
b= b

but I get the following error :

File "/home/thierry/mytest.py", line 20, in ?
print "a:\n"+str(a)
TypeError: 'str' object is not callable

Could someone explain me what I missed ?

Thanks in advance !

hi I am got python 2.4 and changed "class myfather"
to new style classes "class myfather(object)" it worked.
here is the output :

a:
a=a
b=Undefined for mychilda
b:
a=Undefined for mychildb
b=b

But i myself still need explaination ;)

regards
jitu
 
T

tooper

Thanks, at least makes it running !
I'll have to teach myself to move to this new style classes by default
anyway...
 
T

tooper

Not always easy to follow but great !
Using __str__ instead of __repr__ makes it work also with old style
(thanks to Simon Brunning for suggesting it, and with your link I even
now understand why !)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,315
Members
47,996
Latest member
LaurenFola

Latest Threads

Top