Operator Precedence: One Thing Python Got Right

Lawrence D’Oliveiro lawrencedo99 at gmail.com
Mon Jun 27 00:20:54 EDT 2016


There is one thing Python did not slavishly copy from C. While it has (mostly) the same operators, and exclusively adopted the iso646 names for the Boolean operators (which you can also use in C and C++, by the way, but not Java), it made a slight tweak to the operator precedence rules <https://docs.python.org/3/reference/expressions.html#operator-precedence>. Namely, whereas in C, C++ or Java you have to write

    (bitval & bitmask) == needbits

in Python you can dispense with the parentheses

    bitval & bitmask == needbits

How did I discover this? Entirely by accident: I forgot the parentheses one day and *didn’t* hit a bug. :)



More information about the Python-list mailing list