Understanding Python Function Parameter Passing: Value vs. Reference
This article explains how Python implements function argument passing, distinguishing value passing—where a copy of the argument is used—from reference passing for mutable objects, illustrating both mechanisms with swap examples, memory diagrams, and concluding with best practices for modifying data inside functions.
Python determines how function arguments are passed based on the type of the actual parameter: immutable types use value passing, while mutable types use reference passing. This article examines the underlying implementation of both mechanisms.
Value Passing – In value passing, Python creates a copy of the argument and assigns it to the formal parameter, so modifications inside the function do not affect the original variable. The following example demonstrates this behavior:
def swap(a, b):
"""Swap the values of a and b"""
a, b = b, a
print("Inside swap, a =", a, "b =", b)
a = 6
b = 9
swap(a, b)
print("Outside swap, a =", a, "b =", b)Running the code produces:
Inside swap, a = 9 b = 6
Outside swap, a = 6 b = 9The output shows that the outer variables remain unchanged, confirming that the function used value passing.
Figure 1 shows the stack layout of variables a and b in the main program.
Reference Passing – When the actual parameter is a mutable object such as a list or dictionary, Python passes a reference to the same object. The example below swaps two entries in a dictionary:
def swap(dw):
# swap the values of keys 'a' and 'b'
dw['a'], dw['b'] = dw['b'], dw['a']
print("Inside swap, a =", dw['a'], "b =", dw['b'])
dw = {'a': 6, 'b': 9}
swap(dw)
print("Outside swap, a =", dw['a'], "b =", dw['b'])Output:
Inside swap, a = 9 b = 6
Outside swap, a = 9 b = 6Both the inside and outside prints show the dictionary values changed, illustrating that the function operated on the same object via a reference. Internally, Python still uses value passing of the reference variable, but because the reference points to a mutable object, changes are visible outside the function.
Figure 2 visualizes the dictionary reference being passed to the function.
To further demonstrate the separation of variables, the function can set its local reference to None without affecting the original object:
# Inside swap
dw = NoneAfter this assignment, the outer dictionary remains accessible, confirming that only the local reference was cleared.
Conclusions
Assigning a new value to a parameter inside a function does not modify the caller’s variable.
To allow a function to modify data, pass a mutable container (list, dict, etc.) and change its contents within the function.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.