Ternary operator associativity

C

candide

According to the official documentation, the ternary operator has left-to-right associativity :

-------------------
Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right -- see section Comparisons -- and exponentiation, which groups from right to left).
-------------------


Nevertheless, the ternary operator grouping seems to be from right to left, compare :

 
M

Michael Torrie

According to the official documentation, the ternary operator has left-to-right associativity :

I was reading a blog about PHP the other day and it mentioned PHP was
the only language he knew of that had ternary operator precedence going
left to right. All other languages use right to left. So I assume that
Python also uses right to left and that the documentation is a bug.
 
T

Terry Reedy

According to the official documentation, the ternary operator has
left-to-right associativity :

The proper terms is 'conditional expression', which goes back to "The C
Programming Language" (K&R). There are many unary operators, many binary
operators, and there could be other ternary operators.
------------------- Operators in the same box group left to right
(except for comparisons, including tests, which all have the same
precedence and chain from left to right -- see section Comparisons --
and exponentiation, which groups from right to left).
-------------------

Nevertheless, the ternary operator grouping seems to be from right to
left, compare :

True

This behavior is specified in the grammar as given in the C-E section.

The doc is also inconsistent about evaluation order and precedence. I
opened http://bugs.python.org/issue20859 .
 
T

Tim Chase

According to the official documentation, the ternary operator has
left-to-right associativity

I'd never want to rely on my own ability to remember the language
spec, so I strongly advocate for making it explicit with parens
regardless of what the language defines. And that's if I ever created
such a mess in the first place. If you have more than one pair of
conditional expressions in a single assignment, I'd suggest that it's
a code-smell that could use refactoring for clarity/disambiguity.

-tkc
 
T

Terry Reedy

I'd never want to rely on my own ability to remember the language
spec, so I strongly advocate for making it explicit with parens
regardless of what the language defines. And that's if I ever created
such a mess in the first place. If you have more than one pair of
conditional expressions in a single assignment, I'd suggest that it's
a code-smell that could use refactoring for clarity/disambiguity.

It is intended and part of the PEP discussion that one be able to write
chained conditional expression in Python

x = (a1 if c1 else
a2 if c2 else
a3 if c3 else
a4)

without extra ()s, similar to what one can write in C (if I remember
correctly)

x = c1 ? a1 :
c2 ? a2 :
c3 ? a3 :
a4

both being abbreviations for chained if-elses

if c1: x = a1
elif c2: x = a2
elif c3: x = a3
else: x = a4

In all three cases, the conditions are evaluated in 1,2,3 order, each
before the corresponding expression.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top