Pass by Assignment in Python 2026 – Understanding References and Mutability
Python does not use "pass by value" or "pass by reference" like other languages. Instead, it uses **pass by assignment**. Understanding this concept is crucial for writing correct and efficient functions, especially when working with mutable objects.
TL;DR — Key Takeaways 2026
- Python passes objects by assignment: the parameter name is bound to the same object
- Immutable objects (int, str, tuple, etc.) appear to be passed by value
- Mutable objects (list, dict, set, custom classes) can be modified inside functions
- Use
copy()ordeepcopy()when you need to protect original data
1. Core Concept: Pass by Assignment
def modify_list(lst):
lst.append(99) # Modifies the original list
def reassign_list(lst):
lst = [1, 2, 3] # Only rebinds the local name
my_list = [10, 20, 30]
modify_list(my_list)
print(my_list) # [10, 20, 30, 99] ← Changed!
reassign_list(my_list)
print(my_list) # [10, 20, 30, 99] ← Not changed!
2. Immutable vs Mutable Behavior
# Immutable → behaves like pass by value
def increment(n):
n += 1 # Creates a new integer object
x = 5
increment(x)
print(x) # Still 5
# Mutable → can be modified in place
def add_item(items):
items.append("new")
my_list = ["a", "b"]
add_item(my_list)
print(my_list) # ['a', 'b', 'new']
3. Best Practices in 2026
- Be explicit about whether you intend to mutate or rebind
- Use copy() or deepcopy() when you want to protect the original object
- Prefer immutable data structures (tuples, frozensets, NamedTuple) when possible
- Document mutation behavior clearly in function docstrings
- Avoid side effects in pure functions when possible
Conclusion
Understanding "pass by assignment" is fundamental to writing predictable and bug-free Python functions. In 2026, developers who master this concept write clearer code, avoid unexpected mutations, and make better decisions about when to copy data versus mutate in place.
Next steps:
- Review your functions and make mutation behavior explicit
- Related articles: Writing Functions in Python 2026 • Efficient Python Code 2026