Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67947

Re: Ternary operator associativity

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Ternary operator associativity
Date 2014-03-06 13:52 -0500
References <f3bd8da6-dc2e-4b05-b50b-3a2c9d825367@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.7871.1394131996.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web