a *= b not equivalent to a = a*b

Erik python at lucidity.plus.com
Fri Aug 26 03:50:43 EDT 2016


On 26/08/16 08:14, mlzarathustra at gmail.com wrote:
>
> I was being facetious, but behind it is a serious point. Neither the APL nor the J languages use precedence even though their inventor, Ken Iverson, was a mathematician.
>
> That was to support functional programming dating back to the 1970's.

Precedence is not the issue here anyway. Both '*' and '/' have the same 
precedence. The two operations in your expressions have to be evaluated 
in SOME order - either left-to-right or right-to-left.


The issue is twofold:

Firstly, the compound assignments will always evaluate their RHS before 
performing the assignment - this is not the same as operator precedence 
- they have to, else what does Python pass to the function that knows 
what the compound assignment means for that type (e.g. sequences may be 
extended for '*=')? So for compound assignments there is always 
effectively an implicit set of parentheses around the RHS compared to 
the expression without the assignment operator (if you think of the 
compound assignment as being a _part_ of the overall expression).

Secondly, the way you have chosen to layout your code has fooled your 
brain into thinking that the division is performed before the 
multiplication. Python doesn't care that you jammed the sub-expression 
together with no whitespace ;)

E.




More information about the Python-list mailing list