Abstract and concrete syntax

Benji York benji at benjiyork.com
Fri Jun 10 13:56:08 EDT 2005


George Sakkis wrote:
> Another case I've found handy to use lambdas and expressions instead of
> named functions and statements is for simple properties:
> 
> class SomeArray(object):
>     dimensions = property(
>         fget = lambda self: (self.x,self.y),
>         fset = lambda self,(x,y): setattr(self,'x',x) or
>                                   setattr(self,'y',y))

How about:

class SomeArray(object):
     @apply
     def dimensions():
         def fget(self):
             return (self.x, self.y)
         def fset(self, (x, y)):
             self.x = x
             self.y = y
         return property(fget, fset)

It doesn't have the "one-liner" appeal of the lambda version, but it 
reads well.
--
Benji York



More information about the Python-list mailing list