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


Groups > comp.lang.python > #26929

Re: Arithmetic with Boolean values

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 <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; '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 <tjreedy@udel.edu>
Subject Re: Arithmetic with Boolean values
Date Sat, 11 Aug 2012 20:25:51 -0400
References <58f60e60-1dc1-4265-a601-693d11b4bcec@googlegroups.com> <CAPTjJmp9rxwCxS5eeLq54N-csd6MyshCqSEqAjoUrzuj0XUOBg@mail.gmail.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 <CAPTjJmp9rxwCxS5eeLq54N-csd6MyshCqSEqAjoUrzuj0XUOBg@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3200.1344731198.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 8/11/2012 7:13 PM, Chris Angelico wrote:
> On Sun, Aug 12, 2012 at 8:30 AM, John Ladasky
> <john_ladasky@sbcglobal.net> wrote:
>> In [7]: 1 + not(len(L) % 2)
>> ------------------------------------------------------------
>>     File "<ipython console>", 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

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


Thread

Arithmetic with Boolean values John Ladasky <john_ladasky@sbcglobal.net> - 2012-08-11 15:30 -0700
  Re: Arithmetic with Boolean values Chris Angelico <rosuav@gmail.com> - 2012-08-12 09:13 +1000
  Re: Arithmetic with Boolean values Chris Rebert <clp2@rebertia.com> - 2012-08-11 16:53 -0700
  Re: Arithmetic with Boolean values Terry Reedy <tjreedy@udel.edu> - 2012-08-11 20:25 -0400
  Re: Arithmetic with Boolean values Chris Angelico <rosuav@gmail.com> - 2012-08-12 10:31 +1000
  Re: Arithmetic with Boolean values MRAB <python@mrabarnett.plus.com> - 2012-08-12 01:36 +0100
  Re: Arithmetic with Boolean values Paul Rubin <no.email@nospam.invalid> - 2012-08-11 17:54 -0700
    Re: Arithmetic with Boolean values Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-12 11:22 +0000
      Re: Arithmetic with Boolean values Roy Smith <roy@panix.com> - 2012-08-12 07:40 -0400
        Re: Arithmetic with Boolean values Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-12 14:06 +0000
          Re: Arithmetic with Boolean values Paul Rubin <no.email@nospam.invalid> - 2012-08-12 09:59 -0700
            Re: Arithmetic with Boolean values Bernd Nawothnig <Bernd.Nawothnig@t-online.de> - 2012-08-12 19:21 +0200
            Re: Arithmetic with Boolean values Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-12 19:20 +0100
              Re: Arithmetic with Boolean values Roy Smith <roy@panix.com> - 2012-08-12 14:45 -0400
              Re: Arithmetic with Boolean values Alister <alister.ware@ntlworld.com> - 2012-08-12 20:13 +0000
                Re: Arithmetic with Boolean values Gene Heskett <gheskett@wdtv.com> - 2012-08-12 20:29 -0400
                Re: Arithmetic with Boolean values Hans Mulder <hansmu@xs4all.nl> - 2012-08-14 18:32 +0200

csiph-web