Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26927
| References | <58f60e60-1dc1-4265-a601-693d11b4bcec@googlegroups.com> |
|---|---|
| Date | 2012-08-11 16:53 -0700 |
| Subject | Re: Arithmetic with Boolean values |
| From | Chris Rebert <clp2@rebertia.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3199.1344729188.4697.python-list@python.org> (permalink) |
On Sat, Aug 11, 2012 at 3:30 PM, John Ladasky
<john_ladasky@sbcglobal.net> wrote:
<snip>
> for x in range(1 + not(len(L) % 2)):
> # Do stuff
>
> This provoked a SyntaxError. I investigated this further with my interpreter (ipython).
<snip>
> In [5]: not(1)
> Out[5]: False
>
> In [6]: not(len(L) % 2)
> Out[6]: False
>
> In [7]: 1 + not(len(L) % 2)
> ------------------------------------------------------------
> File "<ipython console>", line 1
> 1 + not(len(L) % 2)
> ^
> SyntaxError: invalid syntax
<snip>
> Why is using a logical "not" function, as shown in [7], returning a different result than the test for equivalency as shown in [10]?
Note that, in Python, `not` is an unary operator (it's a language
keyword in fact), as opposed to a function:
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
>>> len("abc")
3
>>> # functions are first-class entities in Python, so we can reference them
>>> len
<built-in function len>
>>> not(1)
False
>>> # but `not` isn't a function
>>> not
File "<stdin>", line 1
not
^
SyntaxError: invalid syntax
>>> # hence why we don't need to call it
>>> not 1
False
The parentheses in `not(foo)` are just grouping the `foo`
subexpression; they're not indicating a function call. Therefore, in
idiomatic Python, such parentheses are omitted whenever possible
(which is the majority of the time), and when they are absolutely
necessary, a space is included before the opening paren, because we're
not making a function call.
Thus, you're simply running into an operator precedence issue, which,
as Chris A. explained, can be remedied by adding grouping parens
around the entire `not` expression; e.g. `(not foo)`
Cheers,
Chris R.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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