[Python-Dev] Definining properties - a use case for class decorators?

Gary Poster gary at modernsongs.com
Sun Oct 16 16:18:54 CEST 2005


On Oct 16, 2005, at 9:56 AM, Nick Coghlan wrote:

> On and off, I've been looking for an elegant way to handle  
> properties using
> decorators.

This isn't my idea, and it might have been brought up here in the  
past to the same sorts of screams of horror to which you refer later,  
but I use the 'apply' pattern without too many internal objections  
for this:

class Foo(object):
     # just a simple example, practically pointless
     _my_property = None
     @apply
     def my_property():
         def get(self):
             return self._my_property
         def set(self, value):
             self._my_property = value
         return property(get, set)

IMHO, I find this easier to parse than either of your two examples.

Apologies if this has already been screamed at. :-)

Gary


More information about the Python-Dev mailing list