conditionals in lambdas?
Alex Martelli
aleaxit at yahoo.com
Thu Nov 9 12:03:02 EST 2000
"Steve Horne" <sh at ttsoftware.co.uk> wrote in message
news:psgl0t4bvnm5o76phoaqqldrrs44uoafmh at 4ax.com...
[snip]
> However, did you realise how easy a lazy version is to create...
No, I hadn't thought of it -- it *is* neat:-).
> Using tuples (which do seem clearer than my list version, and probably
> better in other ways to)...
>
> (no_value,yes_value) [condition]
>
> This can be adapted as follows...
>
> ((lambda : no_value, lambda : yes_value) [condition]) ()
>
> That is, the indexing operation selects which lambda to evaluate.
>
> Of course the lambdas may need some default parameters, and could
> easily get a bit awkward syntactically. But it should work.
The syntax is rapidly headed towards an "Obfuscated Python
Context", yes, but the _concept_ behind it is nevertheless
cool.
> So with the case using division...
>
> ((lambda : 0, lambda j=j : 1/j) [j != 0]) ()
Or, to avoid default-valued parms,
((lambda j:0, lambda j:1/j)[j!=0])(j)
Not that readability and clarity change much, here...:-)
Alex
More information about the Python-list
mailing list