Hard
Engineer
SpeedRun
gotchas
functions
mutability
Python: The Mutable Default Trap
+250 XP · 8 min · attempt 1
Problem
What integer is printed by the snippet below?
def f(x, items=[]):
items.append(x)
return items
a = f(1)
b = f(2)
c = f(3)
print(len(c))
Enter a single integer.
Starter
# Default mutable arguments are evaluated ONCE at def time
# and shared across every call that doesn't pass items explicitly.