Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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; 'python,': 0.02; 'operator': 0.03; 'syntax': 0.03; 'interpreter': 0.04; 'indicating': 0.05; 'function,': 0.07; 'python': 0.09; 'function:': 0.09; 'precedence': 0.09; 'received:mail- vc0-f174.google.com': 0.09; 'cc:addr:python-list': 0.10; 'aug': 0.13; 'language': 0.14; 'sat,': 0.15; '"not"': 0.16; "(it's": 0.16; '2.7.2': 0.16; 'len': 0.16; 'parentheses': 0.16; 'remedied': 0.16; 'subject:values': 0.16; 'syntaxerror:': 0.16; 'unary': 0.16; 'which,': 0.16; 'wrote:': 0.17; 'issue,': 0.17; '>>>': 0.18; '"",': 0.22; 'logical': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; '(which': 0.26; 'opposed': 0.27; 'message- id:@mail.gmail.com': 0.27; 'chris': 0.28; 'grouping': 0.29; 'necessary,': 0.29; 'omitted': 0.29; 'received:209.85.220.174': 0.29; 'thus,': 0.29; 'included': 0.29; "we're": 0.30; 'call.': 0.30; 'e.g.': 0.30; 'keyword': 0.30; 'function': 0.30; 'stuff': 0.30; 'file': 0.32; 'running': 0.32; '11,': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'false': 0.35; 'returning': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'subject:with': 0.36; 'test': 0.36; 'possible': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'space': 0.39; 'header:Received:5': 0.40; 'john': 0.60; 'further': 0.61; 'different': 0.63; 'making': 0.64; 'absolutely': 0.84; 'idiomatic': 0.84; 'investigated': 0.84; 'sender:addr:chris': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=Q/b/ckiht/Vf5tYn9E7oCFj2KYEah+rA2EcYM1NWfgY=; b=CYr3R+Nn1tKEjlnNHy6yEOyr/ARUTsN4yq13c7rvuxBn4mTRbs9HfgfwOF5ZWHZ2hQ Qo8HckSJmoAGaqnFpowVJOKCRXYcKV2LQapkC/Y8PdXs4DHnwj4d1g5OtH3ugTSD/Aza 4K7dTIXqYOgRjleFDCzrhDW+DU5bs80eSXJNQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=Q/b/ckiht/Vf5tYn9E7oCFj2KYEah+rA2EcYM1NWfgY=; b=FzzZC1LRLDHqEXGoeBdaRVE4BQw03sfltYDrJSiMbFxdN0fmTgKPywQYz44dyZHP59 mlLAodixCd2DOAHiCDqcAHBwmGHC7PtkczyhMQzHvuhsH7zm06pbeBX3qdVkWg2O3tlw ROi5cMITWZU6sEwbeRPiOA+fxXH1K6LPDgc1e8iqUG7rwoOWkKrYG2zChQbJ3G3xQtKh wBu4KFUC3QLVT72fpd5KTdlWcWz7IgqWdeYZj5MOniRkY5+c6zFbkrdYbR01KP2PuhZI y1WqeyPWGFLOk4W3b8AhG4m6QtDs3uHCuMB8GZp+H9+BfPLLPLBcIj8OZCpWHeFDkSvk QLag== MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <58f60e60-1dc1-4265-a601-693d11b4bcec@googlegroups.com> References: <58f60e60-1dc1-4265-a601-693d11b4bcec@googlegroups.com> Date: Sat, 11 Aug 2012 16:53:06 -0700 X-Google-Sender-Auth: Tcj2TTbfJ1i9ihGtBLTkdf56sTc Subject: Re: Arithmetic with Boolean values From: Chris Rebert To: John Ladasky Content-Type: text/plain; charset=UTF-8 X-Gm-Message-State: ALoCoQnaZQRclb7WyM0bixje8/wxG+1Je5L2WJg0Uo/p3PSbk+6PrEPRSFymF3kWiuQ9L28Sj4lF Cc: python-list@python.org 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344729188 news.xs4all.nl 6860 [2001:888:2000:d::a6]:42438 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26927 On Sat, Aug 11, 2012 at 3:30 PM, John Ladasky wrote: > for x in range(1 + not(len(L) % 2)): > # Do stuff > > This provoked a SyntaxError. I investigated this further with my interpreter (ipython). > In [5]: not(1) > Out[5]: False > > In [6]: not(len(L) % 2) > Out[6]: False > > In [7]: 1 + not(len(L) % 2) > ------------------------------------------------------------ > File "", line 1 > 1 + not(len(L) % 2) > ^ > SyntaxError: invalid syntax > 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 >>> not(1) False >>> # but `not` isn't a function >>> not File "", 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.