I am new to python. I have a few questions coming from an armature!

Terry Reedy tjreedy at udel.edu
Wed Aug 17 14:27:38 EDT 2016


On 8/17/2016 2:39 AM, Steven D'Aprano wrote:
> On Wednesday 17 August 2016 06:59, Lawrence D’Oliveiro wrote:

>> Here
>> <http://ldo17.tumblr.com/post/17544419704/conditional-expressions-in-python>
>> are some examples of that varying mileage.
>
> Quote:
>
>     The Python syntax for conditional expressions (introduced in Python 2.5) is
>
>         trueval if cond else falseval
>
>     I think this is bloody awful.

When this was initially considered, this was the majority view.

> Why couldn’t they have adopted the standard C
>     syntax, as used in a whole bunch of other C-derivative languages?
>         cond ? trueval : falseval

That particular syntax was not really considered.  At least 10 versions 
using 'if', 'then', 'else', and other tokens were.

They all had the problem of requiring a new keyword such as 'then' or 
some other innovation.

> Because the C syntax is horrifically ugly, whereas the Python syntax is very
> close to real English syntax.
>
> "What will you do tonight?"
>
> "Go to the movies, if I finish work on time, otherwise just go home."

"If I finish work on on time, go to the movies, otherwise just go home."
is also real English syntax, and to me, more graceful.  It is certainly 
more neutral among the alternatives.  The inverted version implies a 
clear preference for the first alternative.

It would be an interesting exercise to see which order for ternary 
expressions is more common in some large corpus of English text.

> Every time you read the C syntax, you lose another three minutes off your
> lifespan. That's how ugly it is.

Every time I write or read the Python syntax chosen, I lose time 
rearranging the terms.

> The background to the Python ternary operator is documented here:
>
> https://www.python.org/dev/peps/pep-0308/

What the *current* version removed from an earlier version is that there 
was a clear community consensus against the condition-in-the-middle 
syntax Guido proposed and for some version of "if condition then 
True-alternative else False-alternative".  Where consensus was lacking 
was which of multiple 'normal order' alternatives to choose.  Part of 
the problem was a lack of knowledge of which alternative Guido might 
accept.  In any case, a runoff vote among the top contenders was not 
allowed.

> Rather than ask why Python uses `trueval if cond else falseval`, you should ask
> why C uses `cond ? trueval : falseval`. Is that documented anywhere?

The ordering of the if-then-else terms is obvious.  '?' ending a 
question (in English) is obvious.  ';' was already used to end 
statememts and could not be used here. Could ',' have been used instead 
of ':'?  I am not sure, but it is used elsewhere in C.  ':' is the only 
other within-Englich-sentence separator available.

The ':' naturally translates to 'else', which was already a keyword. 
There was consensus on this.

The problem for a pythonic (wordy) version of the C expression is that 
the word marker for questions, 'if', normally begins rather than ending 
a question*.  So "cond if trueval else falseval" is likely to be 
misinterpreted.  Hence proposals for "cond then trueval else falseval" 
and "if cond then trueval else falseval" and other variations.

* In a construction like "The cock crows?  If so, I must go, else I 
would tarry with thee longer."  the 'if' follows the question, yet 
cannot standalone but must be followed by something referring back to 
the question.

-- 
Terry Jan Reedy





More information about the Python-list mailing list