Multiline lamba implementation in python.

Josh Gilbert jgilbert.python at gmail.com
Fri Jun 15 22:18:52 EDT 2007


On Wednesday 13 June 2007 4:04 am, Duncan Booth wrote:
> "Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> wrote:
> > But you already have "multiline" lambdas right now in that sense, no
> > need  to add anything. I think you were talking about lambdas *with*
> > statements  inside.
> >
> > bin = lambda x:((x&8 and '*' or '_') +
> >                  (x&4 and '*' or '_') +
> >                  (x&2 and '*' or '_') +
> >                  (x&1 and '*' or '_'))
>
> Or in more recent versions of Python:
>
> bin = lambda x:(('*' if x&8 else '_') +
>                 ('*' if x&4 else '_') +
>                 ('*' if x&2 else '_') +
>                 ('*' if x&1 else '_'))
>
> but seriously, any example of lambda which simply assigns the function to a
> variable is flawed.
>
> I can sort of understand the people who object to a named function taking
> the logic 'out of line', but any expression which actually requires a
> multi-statement function to be embedded in the middle of it is already in
> danger of causing my brain to implode.

You're correct, my lambda function handles statements as well as multiple 
expressions. That code, however, is bloody hideous. If that's what I had to 
do to do multiline callbacks I'd always use def. Ew.



More information about the Python-list mailing list