Inheritance problem ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tooper

    #1

    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(myfath er):
    def __init__(self,a ):
    self.a= a
    def __getattr__(sel f,name):
    return "Undefined for mychilda"

    class mychildb(myfath er):
    def __init__(self,b ):
    self.b= b
    def __getattr__(sel f,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 !

  • db

    #2
    Re: Inheritance problem ?

    On Wed, 24 Aug 2005 03:34:36 -0700, tooper wrote:
    [color=blue]
    > 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(myfath er):
    > def __init__(self,a ):
    > self.a= a
    > def __getattr__(sel f,name):
    > return "Undefined for mychilda"
    >
    > class mychildb(myfath er):
    > def __init__(self,b ):
    > self.b= b
    > def __getattr__(sel f,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 ![/color]

    try new style classes.
    class myfather(object ):

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

    HTH Arjen



    Comment

    • jitya

      #3
      Re: Inheritance problem ?

      tooper wrote:[color=blue]
      > 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(myfath er):
      > def __init__(self,a ):
      > self.a= a
      > def __getattr__(sel f,name):
      > return "Undefined for mychilda"
      >
      > class mychildb(myfath er):
      > def __init__(self,b ):
      > self.b= b
      > def __getattr__(sel f,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 ![/color]

      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

      Comment

      • tooper

        #4
        Re: Inheritance problem ?

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

        Comment

        • jitya

          #5
          Re: Inheritance problem ?

          The stuff on Descriptor.htm was really good .

          Thanks

          Comment

          • tooper

            #6
            Re: Inheritance problem ?

            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 !)

            Comment

            Working...