Skip to content

pySCG: a word about in place operants and unexpected precendence #987

@myteron

Description

@myteron

Operations in Python look like a mathematical formula but follow 'right to left' precedence instead. Provided code example may also be a better solution to XXX-001 Avoid Confusion over the evaluation order.

Augmented assignment is a feature in programming where a binary operation and an assignment are combined into one statement [Python docs 2025 - simple statements]. This approach creates compound operations using operators such as +=, -=, *=, @=, /=, //=, %=, **=, >>=, <<=, &=, ^= or |=.

Consider the following example01.py code:

example01.py:

z = x = 2
x = x * 2 + 1
z *= 2 + 1
print(f"x  = x * 2 + 1={x}")
print(f"z *= 2 + 1    ={z}")

The example01.py output:

x  = x * 2 + 1=5
z *= 2 + 1    =6

The behavior would be considered normal by a programmer while confusion for a mathematician.

We should document this and similar examples as part of the numbers section.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions