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


Groups > comp.lang.python > #109580

Re: Operator precedence problem

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Operator precedence problem
Date 2016-06-06 18:38 +0200
Organization None
Message-ID <mailman.42.1465231123.2306.python-list@python.org> (permalink)
References (6 earlier) <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>

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