Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Variables and name binding

Assignment to a name binds it to the right-hand object without copying that object.

Version: Python 3.14; repository image, production runtime not attested

Syntax

alias = values
values.append(2)
values = [9]

Assignment to a name binds it to the right-hand object without copying that object.

Two names can refer to one list and observe its mutations. Rebinding one name leaves the other binding intact. is compares identity, whereas == tests equality.

After append the alias sees [1, 2]. Rebinding values to [9] leaves alias at [1, 2] and makes alias is values false.

x=[1]
same=x
copy=x.copy()
assert same is x and copy==x and copy is not x
del x
assert same==[1]
a,b=1,2
a,b=b,a
assert (a,b)==(2,1)

Examples

values = [1]
alias = values
values.append(2)
print(alias is values, alias)
values = [9]
print(alias is values, alias, values)

Result

True [1, 2]
False [1, 2] [9]

After append the alias sees [1, 2]. Rebinding values to [9] leaves alias at [1, 2] and makes alias is values false.

Sources