Loading PyCodeIt workspace...
Python functions pass arguments by object reference (`or pass-by-assignment`). Tracing how mutable vs immutable arguments behave when modified inside a function, alongside default parameter evaluation timing (`evaluated exactly once when the function is defined`), is vital for interview mastery.
Mastering Python code tracing requires looking beyond surface syntax to understand how the CPython runtime engine manages call stack frames, variable reference bindings, and object mutability. When you dry-run code mentally, you simulate the exact evaluation sequence executed by the Python bytecode interpreter.
Passing a mutable list into a function is like sharing the key to a storage locker. If the function puts a new box inside the locker (`.append()`), you see the box when you check the locker later. But if the function throws away their key and grabs a key to a new locker (`lst = []`), your original locker remains untouched.
The most notorious Python bug is `def append_to(val, lst=[])`. Because default arguments are evaluated ONCE at function definition time, every call that omits `lst` shares and mutates the exact same default list object across all invocations!
def add_item(val, cache=[]):
cache.append(val)
return cache
print(add_item(1), add_item(2))[1, 2] [1, 2]def process(num, lst):
num += 100
lst.append(99)
x = 5
y = [1, 2]
process(x, y)
print(x, y)5 [1, 2, 99]def connect(host, *, port=8080):
return f'{host}:{port}'
try:
print(connect('localhost', 9000))
except TypeError:
print('Must use port=9000')Must use port=9000count = 10
def check():
try:
print(count)
count = 20
except UnboundLocalError:
print('Local variable referenced before assignment')
check()Local variable referenced before assignmentInteractive Practice
Review the core educational tutorial above and select a question to begin tracing.
Code tracing (or dry-running) is the process of stepping through Python python functions code line-by-line to track variable states, function stack frames, and predict terminal output without running an interpreter.
Tech interviewers test python functions to evaluate if candidates understand core Python memory models, evaluation ordering, and edge-case behavior rather than just memorizing syntax.
Build a 4-column trace table tracking Line Number, Variable Memory, Condition Evaluations (True/False), and Output Buffer. Practice 3-5 trace problems daily on PyCodeIt.