Sort methods - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Sort methods (/thread-27589.html) |
Sort methods - rs74 - Jun-12-2020 a = add(multi(3)); a = a + mod(div(a + 3)) + sub(12) How (and why) should this functions (add, multi, mod, div, sub) be sorted? I am thinking like this -> 1. multi (a = a) (first function) 2. mod (a + mod) (second function) 3. sub (a + mod + sub) 4. ? 5. ? RE: Sort methods - buran - Jun-12-2020 Not clear what you ask. What do you mean by "sorting"? Are these functions from operator module? RE: Sort methods - rs74 - Jun-12-2020 Which function is the first to run? These functions should just be sorted (by me in general, not the program) in a list in the way they are executed(?). Example: multi mod sub div add RE: Sort methods - buran - Jun-12-2020 Read evaluation order and operator precedence Quote:Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. So in your example:
You did not confirm but, I would assume these are functions from operator module, although there is no multi , but mul .So, first of all, your example is invalid, because all these functions expect 2 arguments. e.g. mul(3) will throw error >>> from operator import mul >>> mul(3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: mul expected 2 arguments, got 1 RE: Sort methods - rs74 - Jun-12-2020 (Jun-12-2020, 08:16 AM)buran Wrote: Read evaluation order and operator precedence Thanks. Yes, will read about evaluation order and operator precedence. RE: Sort methods - DeaD_EyE - Jun-12-2020 Code execution is from left to right from top to down. If the left or right operand is a function-call, then this is evaluated first. The operator precedence is what you want to know: https://docs.python.org/3/reference/expressions.html#operator-precedence |