Add and/or alternative and why it doesn't always work.

Correct origin of ?: and ?! again.
This commit is contained in:
Guido van Rossum 2003-02-08 00:56:13 +00:00
parent c486401240
commit edf0585da8
1 changed files with 17 additions and 8 deletions

View File

@ -59,6 +59,15 @@ Proposal
x if C else y, z <==> (x if C else y), z
x, y if C else z <==> x, (y if C else z)
Note: a common way to emulate an if-then-else expression is:
<condition> and <expression1> or <expression2>
However, this doesn't work the same way: it returns <expression2>
when <expression1> is false! See FAQ 4.16 for alternatives that
work -- however, they are pretty ugly and require much more
effort to understand.
Alternatives
@ -66,15 +75,15 @@ Alternatives
<condition> ? <expression1> : <expression2>
I reject this for several reasons: the colon already has many uses
in Python (even though it would actually not be ambiguous, because
the question mark requires a matching colon); for people not used
to C-derived language, it is hard to understand.
Eric Raymond even implemented this. I reject this for several
reasons: the colon already has many uses in Python (even though it
would actually not be ambiguous, because the question mark
requires a matching colon); for people not used to C-derived
language, it is hard to understand.
---
David Ascher proposed, and Eric Raymond even implemented, a
variant that doesn't have this problem:
David Ascher proposed a variant that doesn't have this problem:
<condition> ? <expression1> ! <expression2>
@ -141,8 +150,8 @@ Variations
x = e if C
to several people and they all thought that if C was false, it
would leave x unchanged. So don't even think about this one!
to several people. They all thought that if C was false, it would
leave x unchanged. So don't even think about this one!
Copyright