Problem with Property

none "jay\" at (none)
Sat Feb 25 10:41:57 EST 2006


I'm trying to implement a simple repeateable property mechansism so I 
don't have to write accessors for every single instance variable I have.
------------
classMyObject:
     def __init__ (self):
         self.initialize()

     def initialize(self):
         self._value=None


     def _setProperty (self, name, value):
         print "set property"
         setattr (self, name, value)

     def _getProperty (self, name):
         print "get property"
         return getattr (self, name)

     #properties
     value = property (lambda self: self._getProperty("_value"),
                      lambda self, value: self._setProperty("_value", 
value))


def testObject():
     o = MyObject()
     print o.__dict__
     print o.value
     o.value = 123
     print o.value
     print o.__dict__

if __name__ == "__main__":
     testObject()
---------

The outout looks like this
------------
{'_value': None}
get property
None
123
{'_value': None, 'value': 123}
-----------
As you can see, the _getProperty() method gets called properly when I do 
  'o.value'  but 'o.value = 123' does not seem to use the property 
stucture.   I can't figure out why 'o.value=123' does not call 
_setProperty()

Any ideas?

Jay



More information about the Python-list mailing list