Multiline lamba implementation in python.

Duncan Booth duncan.booth at invalid.invalid
Wed Jun 13 10:04:10 CEST 2007


"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.



More information about the Python-list mailing list