Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'syntax': 0.03; '(except': 0.05; 'binary': 0.05; 'indicated': 0.07; 'parser': 0.07; 'python': 0.09; 'list).': 0.09; 'occasionally': 0.09; 'precedence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'aug': 0.13; '"not"': 0.16; '2))': 0.16; '3.2,': 0.16; 'fwiw': 0.16; 'message-id:@dough.gmane.org': 0.16; 'operator.': 0.16; 'operators.': 0.16; 'parentheses': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sees': 0.16; 'subject:values': 0.16; 'syntaxerror:': 0.16; 'url:py3k': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'appears': 0.18; '(not': 0.20; 'trying': 0.21; 'not,': 0.21; '"",': 0.22; 'interpret': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; '(we': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'chris': 0.28; 'fine': 0.28; 'arithmetic': 0.29; 'enforce': 0.29; 'url:python': 0.32; 'file': 0.32; 'not.': 0.32; 'to:addr :python-list': 0.33; 'same.': 0.35; 'pm,': 0.35; 'received:org': 0.36; 'url:org': 0.36; '12,': 0.36; 'limitation': 0.36; 'subject:with': 0.36; 'subject:: ': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'john': 0.60; 'lower': 0.61; 'worth': 0.63; 'received:fios.verizon.net': 0.84; 'url:reference': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Arithmetic with Boolean values Date: Sat, 11 Aug 2012 20:25:51 -0400 References: <58f60e60-1dc1-4265-a601-693d11b4bcec@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-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344731198 news.xs4all.nl 6878 [2001:888:2000:d::a6]:51940 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26929 On 8/11/2012 7:13 PM, Chris Angelico wrote: > On Sun, Aug 12, 2012 at 8:30 AM, John Ladasky > wrote: >> In [7]: 1 + not(len(L) % 2) >> ------------------------------------------------------------ >> File "", line 1 >> 1 + not(len(L) % 2) >> ^ >> SyntaxError: invalid syntax > > This appears to be a limitation of the parser; it's trying to > interpret "not" as a binary operator. I think not. It is lower precedence than all arithmetic operators. The following is worth knowing about and occasionally reviewing. http://docs.python.org/py3k/reference/expressions.html#summary () around % is not needed; not len(L) % 2 works same. So parser sees 1 + not len(L) % 2 with + given higher precedence than not, So it parses as (1 + not) and croaks, as indicated by caret. (We humans see that that is impossible and may boost the precedence in context.) > 1 + (not(len(L) % 2)) == 1 + (not len(L) % 2) > > Works just fine with parentheses to enforce the correct interpretation. > > This also works in Python 3.2, fwiw (except that you need > list(range(5)) to create the sample list). -- Terry Jan Reedy