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


Groups > comp.lang.python > #67947

Re: Ternary operator associativity

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; '(except': 0.07; 'binary': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:a 10': 0.09; 'tests,': 0.09; 'jan': 0.12; 'comparisons,': 0.16; 'left,': 0.16; 'operators,': 0.16; 'operators.': 0.16; 'precedence': 0.16; 'precedence.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'ternary': 0.16; 'unary': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'programming': 0.22; 'header:User-Agent:1': 0.23; 'section.': 0.24; 'compare': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'url:bugs': 0.29; 'am,': 0.29; 'evaluation': 0.30; 'specified': 0.30; '>>>>': 0.31; 'doc': 0.31; 'grouping': 0.31; 'operators': 0.31; 'url:python': 0.33; '"the': 0.34; 'could': 0.34; 'there': 0.35; 'false': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'according': 0.40; 'chain': 0.60; 'received:173': 0.61; 'back': 0.62; 'skip:n 10': 0.64; 'behavior': 0.77; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Ternary operator associativity
Date Thu, 06 Mar 2014 13:52:46 -0500
References <f3bd8da6-dc2e-4b05-b50b-3a2c9d825367@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-254-207.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
In-Reply-To <f3bd8da6-dc2e-4b05-b50b-3a2c9d825367@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7871.1394131996.18130.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394131996 news.xs4all.nl 2918 [2001:888:2000:d::a6]:51388
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67947

Show key headers only | 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