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


Groups > comp.lang.python > #26929

Re: Arithmetic with Boolean values

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Arithmetic with Boolean values
Date 2012-08-11 20:25 -0400
References <58f60e60-1dc1-4265-a601-693d11b4bcec@googlegroups.com> <CAPTjJmp9rxwCxS5eeLq54N-csd6MyshCqSEqAjoUrzuj0XUOBg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3200.1344731198.4697.python-list@python.org> (permalink)

Show all headers | 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