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 ! 5 999
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
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
Thanks, at least makes it running !
I'll have to teach myself to move to this new style classes by default
anyway...
The stuff on Descriptor.htm was really good .
Thanks
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 !) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: AIM |
last post by:
Error in msvc in building inheritance.obj to build hello.pyd
Hello,
I am trying to build the boost 1.31.0 sample extension hello.cpp.
I can not compile the file inheritance.cpp because the two...
|
by: Graham Banks |
last post by:
Does using multiple inheritance introduce any more performance overhead than
single inheritance?
|
by: JKop |
last post by:
I'm starting to think that whenever you derive one class from another, that
you should use virtual inheritance *all* the time, unless you have an
explicit reason not to. I'm even thinking that...
|
by: Morgan Cheng |
last post by:
It seems no pattern defined by GoF takes advantage of multiple
inheritance. I am wondering if there is a situation where multiple
inheritance is a necessary solution.
When coding in C++, should...
|
by: davidrubin |
last post by:
Structural inheritance (inheriting implementation) is equivalent to
composition in that a particular method must either call 'Base::foo' or
invoke 'base.foo'. Apparantly, The Literature tells us to...
|
by: Steve Jorgensen |
last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling
a particular case of implementation inheritance that would be easy and obvious
in a fully OOP language, but is not at...
|
by: Matthew Louden |
last post by:
I want to know why C# doesnt support multiple inheritance? But why we can
inherit multiple interfaces instead? I know this is the rule, but I dont
understand why. Can anyone give me some concrete...
|
by: Ben Blank |
last post by:
I'm writing a family of classes which all inherit most of their methods and
code (including constructors) from a single base class. When attempting to
instance one of the derived classes using...
|
by: Shawnk |
last post by:
Some Sr. colleges and I have had an on going discussion relative to when and
if
C# will ever support 'true' multiple inheritance.
Relevant to this, I wanted to query the C# community (the...
|
by: Bart Simpson |
last post by:
I remember reading on parashift recently, that "Composition is for code
reuse, inheritance is for flexibility" see
(http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4)
This confused...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |