Abstract and concrete syntax

George Sakkis gsakkis at rutgers.edu
Fri Jun 10 12:53:13 EDT 2005


"Mandus" wrote:

> Thu, 09 Jun 2005 03:32:12 +0200 skrev David Baelde:
> [snip]
> >
> > set_callback(obj,
> >              lambda x: (if a:
> >                2
> >              else:
> >                3)
> >
> [snip]
>
> You can do stuff like this: lambda x: x and 2 or 3
>
> Of course, you are still quite limited in what you can do, but it have
> solved some of my problems...

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

Again, it is quite limited, but properties are also often limited so
it's a useful idiom to know.

George




More information about the Python-list mailing list