Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Operator precedence problem Date: Mon, 06 Jun 2016 18:38:31 +0200 Organization: None Lines: 71 Message-ID: References: <816c651a-d0ae-4e23-a5b2-72a8f7398468@googlegroups.com> <3ff71354-461a-4635-8d1a-c879243e39a8@googlegroups.com> <2a577a90-3a39-4d8f-90fa-4a00fd4f06a3@googlegroups.com> <1465221421.1069788.629246361.60094337@webmail.messagingengine.com> <87inxmmm67.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: news.uni-berlin.de Vc+0BsdPOqD7SqAyn/l5lwLp/iWOtzMAOe4WaX95cXnw== 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; '"""': 0.05; 'python3': 0.05; 'invocation': 0.09; 'negative,': 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; 'tuple': 0.09; 'xor': 0.09; 'python': 0.10; 'assume': 0.11; 'interpreter': 0.15; '"and"': 0.16; '2);': 0.16; '[5]': 0.16; 'bitwise': 0.16; 'boolean': 0.16; 'comparisons,': 0.16; 'conditional': 0.16; 'display,': 0.16; 'division,': 0.16; 'echo': 0.16; 'lambda': 0.16; 'operation.': 0.16; 'positive,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'shifts': 0.16; 'slicing,': 0.16; 'subtraction': 0.16; 'tripped': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'tests': 0.18; 'not,': 0.22; 'shifting': 0.22; 'subject:problem': 0.22; "python's": 0.23; 'unlike': 0.23; 'discussion': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'checking': 0.27; 'not.': 0.27; 'comparison': 0.29; 'dictionary': 0.29; 'operators': 0.29; 'sure,': 0.29; 'that.': 0.30; 'skip:[ 10': 0.31; 'operations': 0.31; 'getting': 0.33; 'url:python': 0.33; 'usually': 0.33; 'call,': 0.33; 'membership': 0.34; 'this?': 0.34; 'list': 0.34; 'functions.': 0.35; 'happened': 0.35; 'identity': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'lines': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'display': 0.37; 'received:org': 0.37; 'aspects': 0.37; 'along': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'url:3': 0.60; 'linked': 0.63; 'more': 0.63; 'goal': 0.64; 'binding': 0.66; 'importantly,': 0.66; '\xe2\x80\x93': 0.72; 'await': 0.76; 'obvious': 0.76; 'url:reference': 0.91; 'confidence': 0.95 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8683.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <816c651a-d0ae-4e23-a5b2-72a8f7398468@googlegroups.com> <3ff71354-461a-4635-8d1a-c879243e39a8@googlegroups.com> <2a577a90-3a39-4d8f-90fa-4a00fd4f06a3@googlegroups.com> <1465221421.1069788.629246361.60094337@webmail.messagingengine.com> <87inxmmm67.fsf@elektro.pacujo.net> Xref: csiph.com comp.lang.python:109580 Marko Rauhamaa wrote: > Random832 : > >> Sure, it's obvious to _me_ that << and >> have higher precedence than & >> and |, and that "and" has a higher precedence than "or", but can I >> assume the other people know this? > > No need to assume. Just read the spec: > > lambda Lambda expression > if – else Conditional expression > or Boolean OR > and Boolean AND > not x Boolean NOT > in, not in, is, is not, <, <=, >, >=, !=, == > Comparisons, including membership tests and identity > tests > | Bitwise OR > ^ Bitwise XOR > & Bitwise AND > <<, >> Shifts > +, - Addition and subtraction > *, @, /, //, % Multiplication, matrix multiplication division, > remainder [5] > +x, -x, ~x Positive, negative, bitwise NOT > ** Exponentiation [6] > await x Await expression > x[index], x[index:index], x(arguments...), x.attribute > Subscription, slicing, call, attribute reference > (expressions...), [expressions...], {key: value...}, {expressions...} > Binding or tuple display, list display, dictionary > display, set display > > or-precedence> > >> [To keep this on-topic, let's assume that this discussion has a goal of >> getting something along the lines of "always/sometimes/never use >> "unnecessary" parentheses" into PEP7/PEP8. Speaking of, did you know >> that C has lower precedence for the bitwise operators &^| than for >> comparisons? That was something that tripped me up for a very long time >> and undermined my confidence as to other aspects of the bitwise >> operators] > > Yes, I happened to know that. Python's the same way. It's not. From the page you linked to: """ Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. """ > However, no need to memorize. It's all there in the spec. Same as with > stdlib functions. Keep checking the spec. Nah, I usually try it in the interactive interpreter: $ python3 -c 'print(1 < 3 & 2)' True $ echo 'main() { printf("%d\n", 1 < 3 & 2); }' | tcc -run - 0 (tcc invocation courtesy of google/stackoverflow) > You *can* assume other people have read the spec. Even more importantly, > you can assume the Python interpreter complies with the spec.