Search code examples
pythonoperatorsvariable-assignmentassignment-operatoroperator-precedence

What is the operator precedence of "=" in Python?


Python's documentation doesn't mention the operator precedence of =. So what is it?


Solution

  • = is not an operator. = is an assignment statement.

    Because it is a statement, it can't be part of an expression (expressions are instead part of certain statements, and never the other way around), so ordering is irrelevant. The expression is always executed to serve a statement.

    For assignments, the grammar specifies that specific types of expressions are permitted after the = symbol:

    assignment_stmt ::=  (target_list "=")+ (starred_expression | yield_expression)
    

    and the documentation for that statement details what order things are executed in:

    An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.