Names point to objects
🌍 In real life. A cricket bat lies in the club room. One player calls it "Rimi's bat" and another calls it "the team bat". Two names, one bat: if someone tapes the handle, both names now refer to a taped bat. Giving the bat a second name didn't make a second bat.
Python names work exactly like that.
The idea in one sentence: a variable is a name tag with an arrow to an object, so b = a doesn't copy anything: it ties a second tag to the same object.
Its words:
- object: a value living in memory, like a list
- reference (the arrow): what a name holds: where its object is
- alias: a second name for the same object
==asks "equal values?";isasks "the same object?"
Step by step, for a = [1, 2], b = a, b.append(3):
a = [1, 2]makes a list and points the tagaat it.b = apoints a second tag,b, at the same list.b.append(3)changes that one list, soasees[1, 2, 3]too.
In earlier chapters we drew variables as boxes. That picture works for numbers and strings, but for lists the arrow picture is the true one.
a = [1, 2]
b = a
b.append(3)
print(a)
print(a is b)Two different tests:
a == basks "do they hold equal values?"a is basks "are they the same object?"
Two separate lists can be equal without being the same list.
a = [1, 2]
b = [1, 2]
c = a
print(a == b, a is b)
print(a == c, a is c)id(x) gives the object's identity number. The number itself changes from run to run, but two names point to the same object exactly when their ids are equal:
a = [1, 2]
b = [1, 2]
c = a
print(id(a) == id(c))
print(id(a) == id(b))Why Python works this way. Copying a big list every time you give it a second name would be slow and would waste memory. Pointing a new tag at the same object is instant, whatever the size. The price is that you have to know when two names share an object.
Mistakes and edge cases.
- "Backing up" with =.
backup = originalmakes an alias, not a backup: changes to one show up in the other (see the bug question). Lesson 13.3 shows real copies. - is for values.
[1] is [1]isFalse: two separate lists, even though they're equal. - Expecting the same id every run.
id()numbers change between runs; only compare ids within one run.
Quiz
What does this print?
a = [1] b = a b.append(2) print(a)What does this print?
a = [1] b = [1] print(a is b)What does this print?
a = [1] b = [1] print(a == b)What does
b = ado whenais a list?- Makes b another name for the same list
- Makes a copy of the list for b
- Moves the list from a to b
What does this print?
a = [1, 2] c = a c[0] = 9 print(a[0])backup should still be [1, 2], but this prints [1, 2, 3]. Which line causes it?
original = [1, 2] backup = original original.append(3) print(backup)
Coding tasks
Start with
a = [1, 2]. Makebanother name for the same list (not a copy), append 3 to it throughb, then printa.Here are two lists:
a = [1, 2]andb = [1, 2]. On one line, print the result ofa == b, then the result ofa is b.Ask for whole numbers on one line and put them in a list called
original. Writealias = original. Then double every number throughalias(changealias[i]for each position), and printoriginal.