Inheritance problem ?

jitya jitendran at productdossier.com
Wed Aug 24 07:48:07 EDT 2005


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




More information about the Python-list mailing list