1.How Python passes objects to functions
Python is neither "pass-by-value" nor "pass-by-reference" — calling a function binds the parameter name to the SAME object the argument expression evaluated to, the same way a plain assignment would. What happens next depends only on whether that object is mutable, and whether the function reassigns the parameter or mutates the object in place.
Key features
- A call binds each parameter name to the argument object itself — identical to name = argument
- Reassigning a parameter inside the function only rebinds that local name; the caller's name is untouched
- Mutating the parameter in place (append, [i]=, attribute set) changes the one shared object — visible to the caller
- Whether a call "looks like" pass-by-value or pass-by-reference depends entirely on the object's mutability, not on how it was passed
- This is why mutable default arguments leak state: the default object is bound once, at def time, and every call that skips the argument shares it
Command
def f(x):
x = x + [1] # rebinds the LOCAL name x — caller's list is untouched
def g(x):
x.append(1) # mutates the SHARED object — caller sees it tooA call is assignment: parameter = argument. Mutable + mutated in place -> caller sees it. Reassigned -> caller never sees it, mutable or not.