Vote on PEP 308: Ternary Operator
Clark C. Evans
cce at clarkevans.com
Wed Mar 5 02:16:32 CET 2003
On Sun, Mar 02, 2003 at 09:30:03PM +0000, Dennis Reinhardt wrote:
| 308 represents too much cruft. Most examples look good as one-liners with
| short names. They do not look so good with longer names. Here is an
| z_of_useful_length = 1.0 + (if abs(z_of_useful_length) < .0001: 0 else:
| The above long-name can be expressed in current Python in nearly the same
| number of characters as
| if abs(z_of_useful_length) < .0001:
| z_of_useful_length = 1.0
| z_of_useful_length += 1.0
| ... much more readable.
Agreed. But I think you are throwing out the baby with the
bathwater here. What is useful about the terinary operator
is that for assignments it is clear that the lhs is uniform.
z_of_useful_length = select abs(z_of_useful_length)
case < .0001: 1.0
else: 1.0 + z_of_useful_length
In this case it is clear that you are making an assignment
with two choices. This syntax also extends nicely to
facilitate more than one case.
More information about the Python-list