Medium10 minPython Fundamentals
UpdatedAug 4, 2026
Edit

Argument Passing and Mutability

CONCEPTS:Python Argument Passing

Question Variations

  • "Is Python pass-by-value or pass-by-reference?"
  • "Why can a function append to my list but not change my integer?"
  • "What is the difference between mutation and rebinding?"
  • "How can a function avoid changing a caller's collection?"

Why This Is Asked

This question reveals whether you can accurately predict the effects of a function call on caller-owned data. It avoids misleading labels such as “pass by reference” by focusing on object bindings, rebinding, and mutation.

Key Concepts

  • Function parameters are local names bound to the argument objects.
  • Mutating a shared mutable object is visible through other references to it.
  • Rebinding a parameter only changes the local name.
  • APIs should document whether they mutate supplied collections.

Question Variations

  • “Is Python pass-by-value or pass-by-reference?”
  • “Why can a function append to my list but not change my integer?”
  • “What is the difference between mutation and rebinding?”
  • “How can a function avoid changing a caller’s collection?”

Answers by Technology

+ Add Variant
PythonImprove this answer ✏️

Expected Answer (Python 3.14)

Python passes references to objects (often described as call-by-object-sharing). The parameter is a local name bound to the same object as the caller’s argument. Mutating that object affects what the caller observes, but assigning the parameter to a different object only changes the local binding.

def update(items, count):
    items.append("new")   # Mutates the caller's list.
    count += 1             # Rebinds only the local name.

items = ["old"]
count = 1
update(items, count)

assert items == ["old", "new"]
assert count == 1

The same behavior applies to every object type; the visible difference comes from whether an operation mutates an object or creates and binds a replacement object.

Why It Matters

Unclear mutation contracts cause bugs at API boundaries, especially when a helper silently changes a shared configuration or payload. Understanding the model makes defensive copying and in-place APIs deliberate choices.

Common Mistakes

  • Calling this ordinary pass-by-reference: A function cannot rebind the caller’s variable through its parameter.
  • Assuming immutable arguments are copied: They can be shared too; their operations simply produce replacement values.
  • Mutating a supplied list without documenting it: Callers may reuse that list and observe surprising changes.

Follow-up Questions

  • How can a function preserve an input list? (Answer: Return a new list or make a shallow/deep copy as appropriate.)
  • Does += always mutate an object? (Answer: No; mutable types may implement in-place addition, while immutable types produce a new object.)