(test) ? a:b

Chris Angelico rosuav at gmail.com
Sat Oct 25 07:25:59 CEST 2014


On Sat, Oct 25, 2014 at 4:03 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Ha! And yet people have, and continue to, complain *bitterly* about the
> non-standard ordering of Python's ternary if, compared to C, standard
> if...else syntax, and English.
>
> "If the syntax is like C, then people will use it, or else they will
> complain that the syntax is incomprehensible."
>
> Personally, I don't accept or agree with such complaints. Python's ternary
> if follows the same syntax as this English variant:
>
> "People will use it if the syntax is like C, or else they will complain that
> the syntax is incomprehensible."

Partly, I think this is a difference between programming and pure
mathematics. In algebra, there's operator precedence, but no order of
evaluation; in programming, there's both:

https://docs.python.org/3/reference/expressions.html#evaluation-order

In English, there's not really a concept of order of evaluation
either. "People will use it" doesn't need to be evaluated separately
from "if the syntax is like C". But in Python, the if/else operator
mucks up the general principle that an expression will be evaluated
left-to-right, because it's evaluated middle-to-outer for that one
operator. Messing with internal expectations, even if it's following
some external logic like English grammar, will confuse people.

ChrisA



More information about the Python-list mailing list