Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67933 > unrolled thread
| Started by | candide <c.candide@laposte.net> |
|---|---|
| First post | 2014-03-06 03:34 -0800 |
| Last post | 2014-03-06 15:15 -0500 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
Ternary operator associativity candide <c.candide@laposte.net> - 2014-03-06 03:34 -0800
Re: Ternary operator associativity Michael Torrie <torriem@gmail.com> - 2014-03-06 10:47 -0700
Re: Ternary operator associativity Terry Reedy <tjreedy@udel.edu> - 2014-03-06 13:52 -0500
Re: Ternary operator associativity Tim Chase <python.list@tim.thechases.com> - 2014-03-06 12:59 -0600
Re: Ternary operator associativity Terry Reedy <tjreedy@udel.edu> - 2014-03-06 15:15 -0500
| From | candide <c.candide@laposte.net> |
|---|---|
| Date | 2014-03-06 03:34 -0800 |
| Subject | Ternary operator associativity |
| Message-ID | <f3bd8da6-dc2e-4b05-b50b-3a2c9d825367@googlegroups.com> |
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 : >>> p = 0 if 1 else 0 if 0 else 1 >>> p 0 >>> left_to_right = (0 if 1 else 0) if 0 else 1 >>> right_to_left = 0 if 1 else (0 if 0 else 1) >>> p == left_to_right False >>> p == right_to_left True >>>
[toc] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2014-03-06 10:47 -0700 |
| Message-ID | <mailman.7869.1394128061.18130.python-list@python.org> |
| In reply to | #67933 |
On 03/06/2014 04:34 AM, candide wrote: > 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 : 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.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-03-06 13:52 -0500 |
| Message-ID | <mailman.7871.1394131996.18130.python-list@python.org> |
| In reply to | #67933 |
On 3/6/2014 6:34 AM, candide wrote: > 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 : > >>>> p = 0 if 1 else 0 if 0 else 1 p > 0 >>>> left_to_right = (0 if 1 else 0) if 0 else 1 >>>> right_to_left = 0 if 1 else (0 if 0 else 1) >>>> p == left_to_right > False >>>> p == right_to_left > 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 . -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-03-06 12:59 -0600 |
| Message-ID | <mailman.7872.1394132381.18130.python-list@python.org> |
| In reply to | #67933 |
On 2014-03-06 03:34, candide wrote: > According to the official documentation, the ternary operator has > left-to-right associativity > > >>> left_to_right = (0 if 1 else 0) if 0 else 1 > >>> right_to_left = 0 if 1 else (0 if 0 else 1) 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
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-03-06 15:15 -0500 |
| Message-ID | <mailman.7873.1394136963.18130.python-list@python.org> |
| In reply to | #67933 |
On 3/6/2014 1:59 PM, Tim Chase wrote:
> On 2014-03-06 03:34, candide wrote:
>> According to the official documentation, the ternary operator has
>> left-to-right associativity
>>
>>>>> left_to_right = (0 if 1 else 0) if 0 else 1
>>>>> right_to_left = 0 if 1 else (0 if 0 else 1)
>
> 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.
--
Terry Jan Reedy
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web