Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this kind of assignment in Python called? a = b = True

I know about tuple unpacking but what is this assignment called where you have multiple equals signs on a single line? a la a = b = True

It always trips me up a bit especially when the RHS is mutable, but I'm having real trouble finding the right keywords to search for in the docs.

like image 480
pfctdayelise Avatar asked Jul 16 '12 05:07

pfctdayelise


People also ask

What are assignments in Python?

The assignment operator, denoted by the “=” symbol, is the operator that is used to assign values to variables in Python. The line x=1 takes the known value, 1, and assigns that value to the variable with name “x”.

What symbol is used for assignment in Python?

Python has an assignment operator that helps to assign values or expressions to the left-hand-side variable. The assignment operator is represented as the "=" symbol used in assignment statements and assignment expressions.

What is a valid assignment statement in Python?

We use Python assignment statements to assign objects to names. The target of an assignment statement is written on the left side of the equal sign (=), and the object on the right can be an arbitrary expression that computes an object.


1 Answers

It's a chain of assignments and the term used to describe it is...

- Could I get a drumroll please?

Chained Assignment.


I just gave it a quite google run and found that there isn't that much to read on the topic, probably since most people find it very straight-forward to use (and only the true geeks would like to know more about the topic).


In the previous expression the order of evaluation can be viewed as starting at the right-most = and then working towards the left, which would be equivalent of writing:

b = True a = b 

The above order is what most language describe an assignment-chain, but python does it differently. In python the expression is evaluated as this below equivalent, though it won't result in any other result than what is previously described.

temporary_expr_result = True  a = temporary_expr_result b = temporary_expr_result 

Further reading available here on stackoverflow:

  • How do chained assignments work? python
like image 170
Filip Roséen - refp Avatar answered Sep 18 '22 20:09

Filip Roséen - refp