Using 'apply' as a decorator, to define constants

Jonathan Fine jfine at pytex.org
Sat Aug 22 05:51:27 EDT 2009


Steven D'Aprano wrote:

> There's a standard idiom for that, using the property() built-in, for 
> Python 2.6 or better.
> 
> Here's an example including a getter, setter, deleter and doc string, 
> with no namespace pollution, imports, or helper functions or deprecated 
> built-ins:
> 
> class ColourThing(object):
>     @property
>     def rgb(self):
>         """Get and set the (red, green, blue) colours."""
>         return (self.r, self.g, self.b)
>     @rgb.setter
>     def rgb(self, rgb):
>         self.r, self.g, self.b = rgb
>     @rgb.deleter
>     def rgb(self):
>         del self.r, self.g, self.b


Sorry, Steve, but I don't understand this.  In fact, I don't even see 
how it can be made to work.

Unless an exception is raised,
     @wibble
     def wobble():
         pass
will make an assignment to wobble, namely the return value of wibble. 
So in your example above, there will be /three/ assignments to rgb. 
Unless you do some complicated introspection (and perhaps not even then) 
surely they will clobber each other.

I still prefer:
     @wibble.property
     def rgb():
         '''Red Green Blue color settings (property)'''

         def fset(rgb):
             self.r, self.g, self.b = rgb
         return locals()

-- 
Jonathan



More information about the Python-list mailing list