Sub topic 8
What happens to an argument when you pass it
This chapter assumes you can write a function with def, call it with arguments, and use lists and tuples. It builds on the earlier chapter on local scope: parameters are local names, and this chapter explains what those names point to.
Passing object references
Python does not copy an argument into the function, and it does not hand over the caller's variable. It binds the parameter name to the same object the caller passed. This is often called passing object references.
def is_same(first, second):
return first is second
data = [1, 2, 3]
print(is_same(data, data)) # TrueInside the function, first and second are new local names for the one list. The is operator checks whether two names refer to the same object.
Rebinding versus mutating
What the function does with the parameter decides whether the caller notices. There are two different operations.
- Rebinding assigns a new object to the parameter name. The caller's variable is unaffected.
- Mutating changes the object in place, for example with
append. Every name that refers to the object sees the change.
def rebind(numbers):
numbers = [0] # the local name now points to a new list
def mutate(numbers):
numbers.append(4) # changes the shared list
data = [1, 2, 3]
rebind(data)
print(data) # [1, 2, 3]
mutate(data)
print(data) # [1, 2, 3, 4]rebind only moves its own local name to a new list. The caller's data still points to the original. mutate changes the object both names share.
Immutable arguments
Numbers, strings and tuples are immutable, which means they cannot be changed in place. Operations that look like changes create a new object and rebind the name.
def add_one(n):
n += 1 # builds a new int and rebinds the local name n
return n
x = 5
print(add_one(x)) # 6
print(x) # 5For this reason, a function cannot change a caller's integer or string by modifying its parameter. It has to return the new value.
Protecting the caller's data
If a function should not change what it receives, work on a copy and return the result.
def with_item(items, item):
result = items.copy() # a new list, so the original is untouched
result.append(item)
return result
original = [1, 2]
extended = with_item(original, 3)
print(original) # [1, 2]
print(extended) # [1, 2, 3]copy() makes a shallow copy. It creates a new outer list, but the elements inside are still shared. If the list contains other lists, changing an inner list is visible through both. Use copy.deepcopy when you need fully independent data.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Reassigning a parameter and expecting the caller to change | The caller's variable is unchanged | Return the new value and assign it |
| Calling `append` or `sort` on a parameter by accident | The caller's list changes | Copy first, or document the change |
| Assuming `.copy()` copies nested data | Inner objects are still shared | Use `copy.deepcopy` |
| Setting `items=[]` as a default | All calls share one list | Use `None` and create the list inside |
The last row is the mutable default pitfall, which gets its own chapter. It is the same mechanism: a mutable object that several names share.
A practical rule
Decide whether a function returns a result or changes its argument, and keep to that choice. If it mutates an argument, say so in the docstring and consider returning None, as list.sort() does. If it returns a result, leave the arguments alone.
Exercise
What do the two print calls show? def reset(values): values = [] def clear(values): values.clear() a = [1, 2] b = [1, 2] reset(a) clear(b) print(a) print(b)
Show answer
The first call prints [1, 2] and the second prints []. In reset, the assignment only rebinds the local name values to a new empty list, so a still points to the original list. In clear, values.clear() mutates the shared list in place, so b is emptied.