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

Tim Peters at
Mon Feb 10 21:24:52 CET 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