12

I have overloaded some Python operators, arithmetic and boolean. The Python precedence rules remain in effect, which is unnatural for the overloaded operators, leading to lots of parentheses in expressions. Is there a way to "overload" Python's precedences?

4
  • 5
    No. It's part of the python language itself. Thats how the language parses. Commented Aug 4, 2012 at 18:28
  • 1
    @jdi That's an answer, why not make it one? Commented Aug 4, 2012 at 18:30
  • @kojiro: Well only because I couldn't find a link to official python docs stating that it can't be changed. I found tons of 3rd party links stating it though. Commented Aug 4, 2012 at 18:33
  • I came across the same problem while trying to increase precedence of xor operator when implementing geometric algebra. Behold, it was Professor Macdonald himself who asked this same question much earlier. Commented Jul 23, 2023 at 14:20

2 Answers 2

15

You can cheat that mechanism in this way:

  1. Override all operators to not do the calculations but create list of instructions wrapped in some object.
  2. Add your own bracket operator (ie. as a _ function).

Example:

>>> a = MyNumber(5); b = MyNumber(2); c = MyNumber(3)
>>> a + b * c
MyExpression([MyNumber(5), '+', MyNumber(2), '*', MyNumber(3)])

Brackets:

>>> a + _(b * c)

Note that _ is a function that evaluates expression (in order you enforce in it)

So if you reverse priorites you will get:

>>> _(a + b * c)
MyNumber(21)

PS. Django does similar trick with Q and F operators.

Sign up to request clarification or add additional context in comments.

Comments

9

No. It's part of the python language itself. Thats how the language parses.

Official quote: Evaluation order

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

Other quotes:

Python:Basics:Numbers and operators

When performing mathematical operations with mixed operators, it is important to note that Python determines which operations to perform first, based on a pre-determined precedence. This precedence follows a similar precedence to most programming languages.

Python Programming/Operators

Note that Python adheres to the PEMDAS order of operations.

5 Comments

The main problem is that python applies operator precedence while parsing code. At that early point it is not possible for python to know what types the objects are actually involved in the expression (as the code has not been executed yet). Therefore if it was possible to change operator precedence, you would do that for all expressions, including common math expressions. Now imagine what kind of chaos would ensue in standard library and in any third-party library if this was possible.
@liori: Nice addition! You could not use any 3rd party code mixed with yours under the same altered interpreter
Thank you jdi for not just stating what Python does, but explaining why it is not possible to do what I would like to do.
However "impossible" is to hard given Python's extreme introspection capabilities. Check Tomasz answer bellow to see an idea of "overriding" operators. In short: create "lazyy operation" objects instead of getting to the final result, and figure out a trigger to perform the actual calculation.
@jsbueno: I acknowledge that there are ways to hack around it. But that is still not effectively changing the operator precedence of python in any way. That is deferring the evaluation to a point where you can then intervene and evaluate it in your own way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.