For review: PEP 308 - If-then-else expression

Tim Peters tim.one at comcast.net
Mon Feb 10 15:24:52 EST 2003


[Michael Hudson]
> This has been an eye-opener for me: people really do write
>
>     x and y or b
>
> I don't know whether I'm more surprised or disappointed.

I do <wink>:  you're more surprised.  You'd be more disappointed if people
actually wrote

    (x and (y,) or (b,))[0]

outside of Usenet posts.  Offhand, at first glance I don't find

    y if x else b

any more sensible than

    x and y or b

At least the meaning of the latter follows directly from what Python's "and"
and "or" mean in isolation.  In that sense it's "an idiom" much as

    (n + 16)//17

is "an idiom" for finding the ceiling of n/17 without using math.ceil() and
int<->float conversions.






More information about the Python-list mailing list