Fire event when variable is Set/Get

Benji York benji at benjiyork.com
Mon Jul 25 07:36:20 EDT 2005


tharaka wrote:
> You are in luck because Python has "Properties" just like .NET.

> 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.")

Just for those that might have tastes like mine; here's the pattern I've 
been using for this (in 2.4, obviously):

class C(object):

     @apply
     def x():
         doc = "I'm the 'x' property."
         def fget(self): return self.__x
         def fset(self, value): self.__x = value
         def fdel(self): del self.__x
     return property(**locals())

You can remove any of fget, fset, fdel, or doc without changing any 
other lines, and there are no "extra" entries in the class's name space.

--
season-to-taste-ly yours,
Benji York



More information about the Python-list mailing list