Abstract and concrete syntax

George Sakkis gsakkis at rutgers.edu
Fri Jun 10 13:00:20 EDT 2005


"Kay Schluehr" 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
>
> You can also do this
>
>    lambda x: {True:2,False:3}.get(bool(a))
>
> which is both beautiful and pythonic.
>
> Kay

Beauty and 'pythonicity' are in the eye of the beholder; IMO both are
much less obvious than the if/then/else, especially for a beginner or
someone who has never seen these idioms before. I personally don't find
the (ab)use of dictionaries in this case to be more beautiful or
pythonic than the and/or idiom (not to mention the runtime cost of
building a new dictionary every time), but YMMV.

George




More information about the Python-list mailing list