PEP-able? Expressional conditions
Steven Bethard
steven.bethard at gmail.com
Wed Sep 7 19:02:19 EDT 2005
Kay Schluehr wrote:
> Terry Reedy wrote:
>> *If* bool(result_expression_i) == True for all i, (except maybe last
>> default expression), which is true for some actual use cases, then the
>> following expression evaluates to the result corresponding to the first
>> 'true' condition (if there is one) or to the default:
>>
>> c0 and r0 or c1 and r1 or c2 and r2... or default.
>
> O.K. you win. One can complete this particular evaluation scheme by
> introducing a little wrapper:
>
> def Id(val):
> return lambda:val
>
> (c0 and Id(r0) or c1 and Id(r1) or c2 and Id(r2)... or Id(default))()
>
> This works for each sequence r0,r1,... without any restictions.
Or use a list as the "wrapper":
(c0 and [r0] or c1 and [r1] or c2 and [r2] ... or [default])[0]
Not that I'd actually encourage anyone to write this code. ;-)
STeVe
More information about the Python-list
mailing list