Property in derived class

Larry Bates larry.bates at websafe.com`
Fri May 9 18:08:22 EDT 2008


Joseph Turian wrote:
> If I have a property in a derived class, it is difficult to override
> the get and set functions: the property's function object had early
> binding, whereas the overriden method was bound late.
> This was previously discussed:
>    http://groups.google.com/group/comp.lang.python/browse_thread/thread/e13a1bd46b858dc8/9d32049aad12e1c1?lnk=gst#9d32049aad12e1c1
> 
> Could someone demonstrate how to implement the proposed solutions that
> allow the property to be declared in the abstract base class, and
> refer to a get function which is only implemented in derived classes?
> 
> Thanks,
>   Joseph

Sounds a little like you are trying to write Java in Python.

1) You don't need get/set functions to get/set properties in Python.  You just
get the property directly using dotted notation.

2) Properties defined in base class exist in derived class unless you override them.


class foobase(object):
   def __init__(self):
     self.myProperty = 1

class foo(foobase):
   self.__init__(self, myProperty=None)
     foobase.__init__(self)
     if myProperty is not None:
       self.myProperty = myProperty


obj = foo()
print obj.myProperty
 >>> 1

obj = foo(6)
print obj.myProperty
 >>> 6

obj.myProperty = 19
print obj.myProperty
 >>> 10

I hope this was what you were looking for.  If not, I don't understand the question.

-Larry



More information about the Python-list mailing list