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


Groups > comp.lang.python > #67949

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!newsfeed1.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; 'else:': 0.03; 'operator': 0.03; 'elif': 0.05; 'advocate': 0.07; 'explicit': 0.07; 'expressions': 0.07; 'mess': 0.09; 'pep': 0.09; '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; 'python': 0.11; 'jan': 0.12; 'suggest': 0.14; 'chained': 0.16; 'conditional': 0.16; 'expression.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'refactoring': 0.16; 'ternary': 0.16; 'language': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'header:User-Agent:1': 0.23; 'regardless': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tim': 0.29; 'strongly': 0.30; 'chase': 0.31; "i'd": 0.34; 'could': 0.34; 'created': 0.35; 'similar': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'ability': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'according': 0.40; 'expression': 0.60; 'received:173': 0.61; 'first': 0.61; 'making': 0.63; 'such': 0.63; 'more': 0.64; '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 15:15:34 -0500
References <f3bd8da6-dc2e-4b05-b50b-3a2c9d825367@googlegroups.com> <20140306125958.31e096f8@bigbox.christie.dr>
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 <20140306125958.31e096f8@bigbox.christie.dr>
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.7873.1394136963.18130.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394136963 news.xs4all.nl 2893 [2001:888:2000:d::a6]:41093
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67949

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious 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