← Back to course
Python 11-12 / Week 04 / Monday
1/6
Week 04 Β· Objects Working Together

Monday

Two names, one object
// Names, references and objects that hold objects
⏱ about 30 min

Monday: Two Names, One Object

Comet bursts into the maker space with her sketch notebook open. "Pip is ready to carry samples," she says. "But my planning code is acting strange."

"I made a list called spots, then wrote route = spots. I added S03 to route, and now spots has S03 too!"

Wren taps his clipboard. "What does the evidence say?" he asks. "Did you make two lists, or two names?"

Nova hovers over the desk and projects two name tags, each with an arrow. "How can I help?" she asks. "Follow each arrow and see where it lands."

Comet turns to you. "You are our lead programmer. Is it one list or two? Trace it before we run it."

A name refers to an object

In Python, a name refers to an object. When you write route = spots, Python does not copy the list.

Both names now refer to one list. This is called aliasing. A change made through one name shows through the other, because there is only one list.

# alias_list.py
spots = ["S01", "S02"]
route = spots
route.append("S03")
print(spots)
print(route)
print(route is spots)
PREDICT ALIAS_LIST.PY
  • Read the question.
  • Tap your answer.
What does the first line, print(spots), show?
What does the last line, print(route is spots), show?
['S01', 'S02', 'S03']
['S01', 'S02', 'S03']
True

is or ==?

== checks whether two values are equal. is checks identity: whether two names refer to the very same object.

In the shell below, x and y hold equal values, but they are two separate lists.

>>> x = [42, 18]
>>> y = [42, 18]
>>> x == y
True
>>> x is y
False
>>> z = x
>>> z is x
True
StatementTrue or false?
y = x makes a copy of the list x.?
After z = x, the test z is x gives True.?
Two lists with equal values are always the same object.?
== compares values, and is compares identity.?
WHY THIS EXERCISEAssignment adds a name, not a copy. == asks about values, and is asks about the very same object.

Aliases of a Sample

Objects work the same way. b = a gives one Sample a second name.

Calling Sample(...) again makes a brand new object, even when its values are equal to another one.

# alias_sample.py
from sample import Sample

a = Sample("S01", "basalt", 42, 0, 2)
b = a
c = Sample("S01", "basalt", 42, 0, 2)
b.grams = 40
print(a.describe())
print(c.describe())
print(a is b)
print(a is c)
print(a.label == c.label)
What does print(a.describe()) show?
What does print(c.describe()) show?
IDENTITY OR EQUAL VALUES?
  • Read the question.
  • Tap your answer.
What does print(a is c) show?
What does print(a.label == c.label) show?
S01 basalt 40 g at (0, 2)
S01 basalt 42 g at (0, 2)
True
False
True
At your computer
1. Open IDLE. Make a new file in your fieldsim folder, next to sample.py. Type alias_sample.py, save it and press F5.
2. Check that your output matches the five lines above.
3. Change b.grams = 40 to c.grams = 40. Predict which lines change, then run it.
4. Add print(d.describe()) at the end and run it. Read the NameError calmly: no name d was ever made. Then delete that line.

Great tracing. Tomorrow you will hand objects to functions and predict what changes.