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


Groups > comp.lang.python > #109580

Re: Operator precedence problem

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 <mailman.42.1465231123.2306.python-list@python.org> (permalink)
References <816c651a-d0ae-4e23-a5b2-72a8f7398468@googlegroups.com> <nj0kol$ps$1@ger.gmane.org> <mailman.5.1465112166.2306.python-list@python.org> <3ff71354-461a-4635-8d1a-c879243e39a8@googlegroups.com> <CAMQ2MsFfxF0wSL_y9tSFCoJ1BJ+GVqi2qJLTBzs7+BbGRjW3Bw@mail.gmail.com> <mailman.9.1465142763.2306.python-list@python.org> <2a577a90-3a39-4d8f-90fa-4a00fd4f06a3@googlegroups.com> <1465221421.1069788.629246361.60094337@webmail.messagingengine.com> <mailman.37.1465221424.2306.python-list@python.org> <87inxmmm67.fsf@elektro.pacujo.net> <nj48u8$h3f$1@ger.gmane.org>
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 <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; '"""': 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 <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nj48u8$h3f$1@ger.gmane.org>
X-Mailman-Original-References <816c651a-d0ae-4e23-a5b2-72a8f7398468@googlegroups.com> <nj0kol$ps$1@ger.gmane.org> <mailman.5.1465112166.2306.python-list@python.org> <3ff71354-461a-4635-8d1a-c879243e39a8@googlegroups.com> <CAMQ2MsFfxF0wSL_y9tSFCoJ1BJ+GVqi2qJLTBzs7+BbGRjW3Bw@mail.gmail.com> <mailman.9.1465142763.2306.python-list@python.org> <2a577a90-3a39-4d8f-90fa-4a00fd4f06a3@googlegroups.com> <1465221421.1069788.629246361.60094337@webmail.messagingengine.com> <mailman.37.1465221424.2306.python-list@python.org> <87inxmmm67.fsf@elektro.pacujo.net>
Xref csiph.com comp.lang.python:109580

Show key headers only | View raw


Marko Rauhamaa wrote:

> Random832 <random832@fastmail.com>:
> 
>> 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
> 
>    <URL: https://docs.python.org/3/reference/expressions.html#operat
>    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.

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


Thread

Operator precedence problem ICT Ezy <ictezy@gmail.com> - 2016-06-04 23:53 -0700
  Re: Operator precedence problem Peter Otten <__peter__@web.de> - 2016-06-05 09:35 +0200
    Re: Operator precedence problem ICT Ezy <ictezy@gmail.com> - 2016-06-05 08:05 -0700
      Re: Operator precedence problem Uri Even-Chen <uri@speedy.net> - 2016-06-05 19:05 +0300
        Re: Operator precedence problem ICT Ezy <ictezy@gmail.com> - 2016-06-05 20:24 -0700
        Re: Operator precedence problem Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-05 22:46 -0700
          Re: Operator precedence problem Random832 <random832@fastmail.com> - 2016-06-06 09:57 -0400
            Re: Operator precedence problem Marko Rauhamaa <marko@pacujo.net> - 2016-06-06 17:22 +0300
              Re: Operator precedence problem Random832 <random832@fastmail.com> - 2016-06-06 10:35 -0400
                Re: Operator precedence problem Marko Rauhamaa <marko@pacujo.net> - 2016-06-06 17:55 +0300
              Re: Operator precedence problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-06-06 15:03 +0000
                Re: Operator precedence problem Marko Rauhamaa <marko@pacujo.net> - 2016-06-06 18:22 +0300
                Re: Operator precedence problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-06-06 15:27 +0000
                Re: Operator precedence problem Chris Angelico <rosuav@gmail.com> - 2016-06-07 01:57 +1000
                Re: Operator precedence problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-06-06 16:05 +0000
                Re: Operator precedence problem Chris Angelico <rosuav@gmail.com> - 2016-06-07 02:21 +1000
                Re: Operator precedence problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-06-06 17:02 +0000
                Re: Operator precedence problem Grant Edwards <grant.b.edwards@gmail.com> - 2016-06-06 16:51 +0000
              Re: Operator precedence problem Peter Otten <__peter__@web.de> - 2016-06-06 18:38 +0200
            Re: Operator precedence problem Steven D'Aprano <steve@pearwood.info> - 2016-06-07 03:07 +1000
              Re: Operator precedence problem Random832 <random832@fastmail.com> - 2016-06-06 14:44 -0400
                Re: Operator precedence problem Marko Rauhamaa <marko@pacujo.net> - 2016-06-06 21:49 +0300
              Re: Operator precedence problem MRAB <python@mrabarnett.plus.com> - 2016-06-06 20:17 +0100
                Re: Operator precedence problem Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-06-07 11:27 +1200
            Re: Operator precedence problem Rustom Mody <rustompmody@gmail.com> - 2016-06-06 10:14 -0700
              Re: Operator precedence problem Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-06 21:09 +0100
      Re: Operator precedence problem Michael Torrie <torriem@gmail.com> - 2016-06-05 17:47 -0600
        Re: Operator precedence problem ICT Ezy <ictezy@gmail.com> - 2016-06-05 20:23 -0700
  Re: Operator precedence problem Chris Angelico <rosuav@gmail.com> - 2016-06-06 02:24 +1000
    Re: Operator precedence problem Peter Pearson <pkpearson@nowhere.invalid> - 2016-06-06 16:58 +0000
      Re: Operator precedence problem Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-06-07 11:21 +1200
  Re: Operator precedence problem Random832 <random832@fastmail.com> - 2016-06-05 13:19 -0400
    Re: Operator precedence problem ICT Ezy <ictezy@gmail.com> - 2016-06-05 20:23 -0700
  Re: Operator precedence problem Gary Herron <gherron@digipen.edu> - 2016-06-05 00:04 -0700

csiph-web