[Python-Dev] properties with decorators (was: code blocks using 'for' loops and generators)

Nick Coghlan ncoghlan at iinet.net.au
Thu Mar 17 15:50:53 CET 2005

Josiah Carlson wrote:
> Samuele Pedroni <pedronis at strakt.com> wrote:
>>well, I think some people desire a more streamlined way of writing code
>>def f(...)
>>def g(...)
>>x = h(...,f,g)
>>[property, setting up callbacks etc are cases of this]
> I think properties are the most used case where this kind of thing would
> be nice.  Though the only thing that I've ever had a gripe with
> properties is that I didn't like the trailing property() call - which is
> why I wrote a property helper decorator (a use can be seen in [1]).  But
> my needs are small, so maybe this kind of thing isn't sufficient for
> those who write hundreds of properties.

I'm still trying to decide if the following is an elegant solution to defining 
properties, or a horrible abuse of function decorators:

def as_property(func):
   return property(*func())

class Example(object):

   def x():
     def get(self):
       print "Getting!"
     def set(self, val):
       print "Setting!"
     def delete(self):
       print "Deleting!"
     return get, set, delete


Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia

More information about the Python-Dev mailing list