Mixing custom __setattr__ method and properties in new style classes

Fredrik Lundh fredrik at pythonware.com
Wed Feb 8 12:25:25 EST 2006


"L.C. Rees" wrote:

> This is the code for the property at the end of the class definition:
>
>     def _settext(self, txt):
>         self._tree.text = txt
>
>     def _gettext(self, txt):
>         return self._tree.text
>
>     def _deltext(self, txt):
>         self._tree.text = ''
>
>     text = property(_settext, _gettext, _deltext)

hint:

>>> help(property)

class property(object)
 |  property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
 |
 |  fget is a function to be used for getting an attribute value, and likewise
 |  fset is a function for setting, and fdel a function for del'ing, an
 |  attribute.  Typical use is to define a managed attribute x:
 |  class C(object):
 |      def getx(self): return self.__x
 |      def setx(self, value): self.__x = value
 |      def delx(self): del self.__x
 |      x = property(getx, setx, delx, "I'm the 'x' property.")

</F>






More information about the Python-list mailing list