Taken literally, neither a
nor ls[1]
are objects: They are an identifier and subscription, respectively. While they evaluate to objects when used in an expression, that does not make them identical to these objects – and the identity of "their" objects does not extend to them, either.
- The expression
a is ls[0]
means "evaluate a
and ls[0]
and compare the result for identity". It does not check whether the identifier a
is identical to the subscription ls[0]
.
Notably, when used in an assignment statement, both a
and ls[1]
do not represent "an expression to look up an object" but rather "a target to assign an object to". The features of the former do not impact the latter.
The statement a, ls[1] = ls[1], a
uses both a
and ls[1]
as both expressions and targets:
The right hand side is an expression. It represents "a tuple of the results of evaluating ls[1]
and a
".
The left hand side is an assignment. It represents "assign to the identifier a
and the subscription ls[1]
".
Assignment to an identifier always is immediate: it does not matter what value the identifier previously referred to. Specifically, it does not matter that a
previously referred to an object that was also referred to by ls[0]
, and it will not matter that its new referent was also referred to by ls[1]
.
To understand why this distinction is important, consider the case that an identifier "is identical to" a literal:
>>> # integers < 256 are interned
>>> a = 12
>>> # identifier "is identical to" literal
>>> a is 12
True
If identity in expressions would equate to identity in assignments, then setting a = 13
would now reassign both a
and 12
to have the value 13
.