← Back to course
Python 11-12 / Week 04 / Tuesday
2/6
Week 04 · Objects Working Together

Tuesday

Objects as arguments
// Names, references and objects that hold objects
⏱ about 30 min

Tuesday: Objects as Arguments

Comet has found a problem with the maker space scale: it reads two grams heavy. She writes trim(sample) to fix each Sample, and it works.

Then she writes swap_in(sample), which is meant to replace a sample with a fresh one. It runs without an error, and nothing happens.

"Same kind of function," she says, frowning. "Why does one change things and the other does not?"

Wren copies both functions onto graph paper. "What do you notice?" he asks. "One changes the object. The other changes the name."

Nova draws an arrow from s and a second arrow from sample. "Would you like a hint?" she asks. "Watch where each arrow points as the function runs."

A parameter is a new name

Python passes arguments by assignment. The parameter becomes a new name for the same object the caller passed in.

So a function can change the object through its parameter, and the caller sees the change.

But if the function assigns a new object to the parameter, only that name moves. The caller still refers to the old object.

# args.py
from sample import Sample


def trim(sample):
    sample.grams = sample.grams - 2


def swap_in(sample):
    sample = Sample("S09", "chalk", 25)
    sample.grams = 0


s = Sample("S01", "basalt", 42, 0, 2)
trim(s)
print(s.describe())
swap_in(s)
print(s.describe())
Line that runss refers tosample refers toS01 grams
s = Sample("S01", ...)S01 objectno name yet42
trim(s) startsS01 objectS01 object42
sample.grams = sample.grams - 2S01 objectS01 object40
swap_in(s) startsS01 objectS01 object40
sample = Sample("S09", ...)S01 objectnew S09 object40
sample.grams = 0S01 objectS09 object, now 0 g40
PREDICT ARGS.PY
  • Read the question.
  • Tap your answer.
What is the first line of output?
What is the second line of output?
S01 basalt 40 g at (0, 2)
S01 basalt 40 g at (0, 2)

Lists as arguments

Lists behave the same way. Appending through the parameter changes the caller's list. Assigning a new list to the parameter does not.

# list_args.py
def add_spot(spots):
    spots.append("S04")


def new_list(spots):
    spots = ["S99"]


route = ["S01", "S02"]
add_spot(route)
print(route)
new_list(route)
print(route)
print(len(route))
PREDICT LIST_ARGS.PY
  • Read the question.
  • Tap your answer.
What does the second print(route) show?
What does print(len(route)) show?
['S01', 'S02', 'S04']
['S01', 'S02', 'S04']
3
PUT THE STEPS OF ADD_SPOT(ROUTE) IN ORDER
  • ?spots.append("S04") changes the one shared list.
  • ?print(route) shows the list with S04 in it.
  • ?The function ends, and the name spots goes away.
  • ?The parameter spots becomes a second name for that list.
  • ?Python finds the list that route refers to.
WHY THIS EXERCISEThe parameter is a new name for the same list, so the change made inside the function stays after it ends.

Hand the new object back

If a function makes a new object that the caller needs, it should return it, and the caller stores it. Here swap_in ends with return sample, and the call is s = swap_in(s).

# args_return.py
from sample import Sample


def swap_in(sample):
    sample = Sample("S09", "chalk", 25)
    sample.grams = 0
    return sample


s = Sample("S01", "basalt", 42, 0, 2)
s = swap_in(s)
print(s.describe())
PREDICT ARGS_RETURN.PY
  • Read the question.
  • Tap your answer.
What does args_return.py show?
At your computer
1. In your fieldsim folder, type args.py, save it and run it. Check the two lines.
2. Make the change from args_return.py: add return sample to swap_in, and write s = swap_in(s). Run it and check the S09 line.
3. Change trim(s) to trim() with no argument and run it. Read the TypeError calmly: it names the missing argument. Then put s back.
StatementTrue or false?
A function can change an object that is passed to it.?
Assigning a new object to a parameter also moves the caller's name.?
A parameter is a new name for the object the caller passed.?
To hand a new object back, a function can return it.?
WHY THIS EXERCISEArguments are passed by assignment. Changing the object shows outside; moving the parameter name does not.

Nicely traced. Tomorrow Pip gets a cargo bay: a Rover that holds Samples.

← Monday