469,310 Members | 2,365 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,310 developers. It's quick & easy.

Inheritance problem ?

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 !

Aug 24 '05 #1
5 966
db
On Wed, 24 Aug 2005 03:34:36 -0700, tooper wrote:
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

Aug 24 '05 #2
tooper wrote:
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

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

Aug 24 '05 #4
The stuff on Descriptor.htm was really good .

Thanks

Aug 24 '05 #5
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 !)

Aug 24 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Graham Banks | last post: by
4 posts views Thread by JKop | last post: by
5 posts views Thread by Morgan Cheng | last post: by
10 posts views Thread by davidrubin | last post: by
14 posts views Thread by Steve Jorgensen | last post: by
22 posts views Thread by Matthew Louden | last post: by
45 posts views Thread by Ben Blank | last post: by
6 posts views Thread by Bart Simpson | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.