These are the notes for Python Coding Questions, episode 18: function questions. Interviewers love these because they test whether you know what Python really does when it runs def. First a trap: a default argument that quietly remembers things between calls. Then two interview questions: write a timing decorator (a stopwatch you can clip onto any function), and make a slow recursive function fast with memoization, first with a dictionary and then with functools.lru_cache.
Every program was run with Python 3.13 (python3 main.py), the SyntaxPilot compiler's Python command. Every output below is real (times measured with a clock change a little from run to run, and memory addresses are different every run).
A quick reminder of the words we use. A parameter is the name in the def line; an argument is the value you pass when you call. A default argument is a value written in the def line that is used when the caller leaves that argument out:
def greet(name, greeting="Namaste"):
return f"{greeting}, {name}!"
print(greet("Asha"))
print(greet("Ravi", "Hello"))
Output:
Namaste, Asha!
Hello, Ravi!
The trap: a mutable default argument
Real-life example: a shop's billing software has a function "add an item to a cart". If the customer doesn't bring a cart, it hands them an empty one. Asha comes in and adds milk. Then Ravi comes in, with no cart, and adds bread, and his cart says milk and bread. The shop only ever had one cart behind the counter, and gave that same cart to everyone.
The obvious code: a default argument cart=[].
def add_item(item, cart=[]):
cart.append(item)
return cart
asha = add_item("milk")
ravi = add_item("bread")
print(asha)
print(ravi)
print(asha is ravi)
print(add_item.__defaults__)
Output:
['milk', 'bread']
['milk', 'bread']
True
(['milk', 'bread'],)
Ravi's cart already holds Asha's milk, both names point to the same list (asha is ravi is True), and the function's stored defaults (__defaults__) now hold ['milk', 'bread'].
Why
A default value is created once, when Python runs the def line, and it's stored on the function object. It is not created again on each call. So every call that leaves out cart gets the very same list, and append changes that list for everyone.
This only causes trouble with mutable values: objects that can be changed in place, such as a list, a dict or a set. Numbers, strings, tuples and None are immutable (they can't be changed in place), so they are safe defaults.
You can watch the default being made only once:
def make_default():
print("making the default list")
return []
def add_item(item, cart=make_default()):
cart.append(item)
return cart
print("def is done")
add_item("milk")
add_item("bread")
print(add_item("eggs"))
Output:
making the default list
def is done
['milk', 'bread', 'eggs']
"making the default list" is printed once, before any call.
The fix: None as the default
Use None as the default and make the new list inside the function, where it runs on every call:
def add_item(item, cart=None):
if cart is None:
cart = [] # a NEW list on every call
cart.append(item)
return cart
asha = add_item("milk")
ravi = add_item("bread")
print(asha)
print(ravi)
print(add_item("eggs", ["tea"]))
Output:
['milk']
['bread']
['tea', 'eggs']
Each customer gets their own cart, and a customer who brings a cart (["tea"]) keeps using theirs. Use if cart is None, not if not cart: with if not cart, a caller who passes their own empty list would get a different, new list instead of theirs.
The same trap with a dictionary:
def count_word(word, counts={}):
counts[word] = counts.get(word, 0) + 1
return counts
print(count_word("chai"))
print(count_word("samosa")) # a "fresh" call still remembers chai
Output:
{'chai': 1}
{'chai': 1, 'samosa': 1}
Q1: Write a timing decorator
Real-life example: a chai shop's app is slow, and the owner wants to know which step is slow: making the bill, or printing the receipt. You could write a stopwatch into every recipe, or clip one stopwatch onto any function from the outside, without touching its recipe. That clip-on stopwatch is a decorator.
Attempt 1: the same lines in every function
time.perf_counter() returns a clock reading in seconds, made for measuring short time spans. The obvious way is to put the start, the print and the result-keeping into each function:
import time
def total_bill(prices):
start = time.perf_counter()
result = sum(prices)
print(f"total_bill took {time.perf_counter() - start:.3f} s")
return result
def print_receipt():
start = time.perf_counter()
time.sleep(0.3)
print(f"print_receipt took {time.perf_counter() - start:.3f} s")
return "printed"
print(total_bill([40, 25, 60]))
print(print_receipt())
Output:
total_bill took 0.000 s
125
print_receipt took 0.300 s
printed
It works, but the three timing lines are copied into every function. Twenty functions means twenty copies; turning timing off means editing all twenty again; and the timing code is mixed into the real work.
Functions are values
In Python a function is an object, like a number or a list, and its name just points to it. So you can pass a function into another function, and return a new function from one. A function that takes a function and returns a new function is a decorator:
def greet():
print("Namaste!")
def twice(func): # takes a function
def wrapper():
func()
func()
return wrapper # gives back a new one
greet = twice(greet)
greet()
Output:
Namaste!
Namaste!
wrapper is an inner function (a function defined inside another). It can use func even after twice has returned. The @ line is a shortcut: writing @twice above def greet means exactly greet = twice(greet).
def twice(func):
def wrapper():
func()
func()
return wrapper
@twice
def greet():
print("Namaste!")
greet()
print(greet.__name__)
Output:
Namaste!
Namaste!
wrapper
Attempt 2: a wrapper with no parameters
import time
def timed(func):
def wrapper():
start = time.perf_counter()
func()
print(f"took {time.perf_counter() - start:.4f} s")
return wrapper
@timed
def total_bill(prices):
return sum(prices)
print(total_bill([40, 25, 60]))
Output:
Traceback (most recent call last):
File "main.py", line 14, in <module>
print(total_bill([40, 25, 60]))
~~~~~~~~~~^^^^^^^^^^^^^^
TypeError: timed.<locals>.wrapper() takes 0 positional arguments but 1 was given
Why it fails: after the @, the name total_bill points to wrapper. So the prices go to wrapper, and wrapper accepts nothing. The wrapper must accept whatever the real function accepts.
*args and **kwargs
We don't know what the decorated function will take, so accept everything:
*argscollects all positional arguments (passed by position) into a tuple.**kwargscollects all keyword arguments (passed asname=value) into a dictionary.- In a call,
func(*args, **kwargs)unpacks them again, exactly as they came in.
def show(*args, **kwargs):
print(args)
print(kwargs)
show(40, 25, tip=10, note="extra chai")
Output:
(40, 25)
{'tip': 10, 'note': 'extra chai'}
Attempt 3: everything passed through, but the result is lost
import time
def timed(func):
def wrapper(*args, **kwargs):
start = time.perf_counter()
func(*args, **kwargs)
print(f"took {time.perf_counter() - start:.4f} s")
return wrapper
@timed
def total_bill(prices):
return sum(prices)
print(total_bill([40, 25, 60]))
print(total_bill.__name__)
Output:
took 0.0000 s
None
wrapper
Two more bugs:
- The bill prints
None. The wrapper called the function and threw its answer away; a function that ends withoutreturngivesNone. total_bill.__name__is'wrapper'. The name really points to the wrapper now, so logs, error messages andhelp()show the wrong name and docstring.
The fix: keep the result, and functools.wraps
functools.wraps(func) is itself a decorator for the wrapper: it copies the original function's name (__name__), docstring (__doc__, its help text) and a few other details onto the wrapper.
import time
from functools import wraps
def timed(func):
@wraps(func) # keep the real name
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs) # keep what it returns
took = time.perf_counter() - start
print(f"{func.__name__} took {took:.3f} s")
return result
return wrapper
@timed
def total_bill(prices, tip=0):
return sum(prices) + tip
@timed
def print_receipt():
time.sleep(0.3) # a slow printer
return "printed"
print(total_bill([40, 25, 60], tip=10))
print(print_receipt())
print(total_bill.__name__)
Output:
total_bill took 0.000 s
135
print_receipt took 0.300 s
printed
total_bill
The result comes back, tip=10 passes through as a keyword argument, the slow function is caught, and the name is kept. The decorator adds two clock reads per call: O(1) extra time and space.
The shape of every decorator you'll write in an interview:
def deco(func):
@wraps(func)
def wrapper(*args, **kwargs):
# before
result = func(*args, **kwargs)
# after
return result
return wrapper
A decorator that takes arguments
@repeat(3) first calls repeat(3), which must return the real decorator. That's one more level of function:
from functools import wraps
def repeat(times):
def deco(func):
@wraps(func)
def wrapper(*args, **kwargs):
result = None
for _ in range(times):
result = func(*args, **kwargs)
return result
return wrapper
return deco
@repeat(3)
def ring():
print("ring!")
ring()
Output:
ring!
ring!
ring!
Q2: Memoization
Real-life example: in a tuition class, students keep asking the same sums and the teacher solves each from scratch. A smarter teacher writes every solved answer on the board and looks there before solving. Remembering answers so you never compute them twice is memoization; the place you keep them (the board) is a cache. Python's standard library has a ready-made board: functools.lru_cache.
We use the Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, ... where each number is the sum of the two before it, so fib(n) = fib(n - 1) + fib(n - 2), with fib(0) = 0 and fib(1) = 1.
Attempt 1: plain recursion
A recursive function is one that calls itself. A global counter counts every call:
import time
calls = 0
def fib(n):
global calls
calls += 1
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
start = time.perf_counter()
print(fib(32))
print("calls:", calls)
print(f"took {time.perf_counter() - start:.2f} s")
Output:
2178309
calls: 7049155
took 0.40 s
The right answer, but over seven million calls.
Why it's slow
fib(32) asks for fib(31) and fib(30). fib(31) asks for fib(30) again, and fib(29). Each of those asks for fib(29) ... the same sums are solved again and again, and the number of calls almost doubles with every level. That's exponential time, about O(1.6ⁿ) (often written O(2ⁿ) as an upper bound). Small n is fine; each +1 to n makes it about 1.6 times slower.
for n in [10, 20, 25, 30]:
calls = 0
def fib(k):
global calls
calls += 1
return k if k < 2 else fib(k - 1) + fib(k - 2)
fib(n)
print(n, calls)
Output:
10 177
20 21891
25 242785
30 2692537
The fix: look before you solve
Keep a dictionary memo of solved answers. When fib(n) is asked: if n is in memo, return it at once; otherwise solve it once, store it, and return it.
import time
memo = {}
calls = 0
def fib(n):
global calls
calls += 1
if n in memo:
return memo[n] # already solved: look it up
if n < 2:
return n
memo[n] = fib(n - 1) + fib(n - 2)
return memo[n]
start = time.perf_counter()
print(fib(32))
print("calls:", calls)
print(f"took {time.perf_counter() - start:.5f} s")
print(fib(300))
Output:
2178309
calls: 63
took 0.00014 s
222232244629420445529739893461909967206666939096499764990979600
63 calls instead of 7,049,155. Each n from 0 to 32 is solved once, so the time is O(n), and the memo holds n answers: O(n) space. fib(300), a 63-digit number, is instant too: Python integers never overflow.
Only memoize pure functions. A pure function always gives the same output for the same input and changes nothing outside itself. Never cache something that reads the clock, random numbers, a file or a database that changes: the cache would keep returning an old answer.
The built-in board: functools.lru_cache
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
print(fib(32))
print(fib.cache_info())
print(fib(300))
@lru_cache(maxsize=None)
def bill(prices):
return sum(prices)
print(bill((40, 25, 60)))
print(bill([40, 25, 60]))
Output:
2178309
CacheInfo(hits=30, misses=33, maxsize=None, currsize=33)
222232244629420445529739893461909967206666939096499764990979600
125
Traceback (most recent call last):
File "main.py", line 18, in <module>
print(bill([40, 25, 60]))
~~~~^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'
cache_info()reports misses (answers it had to compute: 33, one for each n from 0 to 32) and hits (answers read from the cache: 30).maxsize=Nonemeans "never forget".functools.cache(Python 3.9+) is the same aslru_cache(maxsize=None).- LRU means least recently used: with a size limit, such as the default
maxsize=128, a full cache throws out the answer that was used longest ago. - The cache is a dictionary whose key is the arguments. A key must be hashable: it has a fixed hash value and can't change. A list can change, so
TypeError: unhashable type: 'list'. Pass a tuple instead.
A size limit in action:
from functools import lru_cache
@lru_cache(maxsize=2)
def price(item):
print(" looking up", item)
return {"chai": 15, "samosa": 20, "vada pav": 25}[item]
for item in ["chai", "samosa", "chai", "vada pav", "samosa"]:
print(item, price(item))
print(price.cache_info())
Output:
looking up chai
chai 15
looking up samosa
samosa 20
chai 15
looking up vada pav
vada pav 25
looking up samosa
samosa 20
CacheInfo(hits=1, misses=4, maxsize=2, currsize=2)
samosa was thrown out when vada pav came in (it was the least recently used), so it was looked up again.
Memoizing a function with a list argument
Turn the list into a tuple at the edge, and cache the tuple version:
from functools import lru_cache
@lru_cache(maxsize=None)
def _bill(prices):
print(" adding up", prices)
return sum(prices)
def bill(prices):
return _bill(tuple(prices))
print(bill([40, 25, 60]))
print(bill([40, 25, 60]))
Output:
adding up (40, 25, 60)
125
125
Common mistakes (and what you really see)
| Mistake | What you see | Why | Fix |
|---|---|---|---|
def add_item(item, cart=[]) |
['milk', 'bread'] for both customers |
the default list is made once, at def |
cart=None, then cart = [] inside |
if not cart: instead of if cart is None: |
a caller's own empty list is replaced | [] is also falsy |
if cart is None |
def wrapper(): |
TypeError: timed.<locals>.wrapper() takes 0 positional arguments but 1 was given |
the wrapper receives the arguments | def wrapper(*args, **kwargs) |
calling func(...) without keeping it |
the decorated function returns None |
the result is thrown away | result = func(...), return result |
no @wraps(func) |
f.__name__ is 'wrapper' |
the name now points to the wrapper | @wraps(func) on the wrapper |
@timed() on a decorator that takes no arguments |
TypeError |
timed() is called with no function |
@timed without brackets |
plain recursive fib(32) |
7,049,155 calls, a long wait | the same sums again and again | a memo dict or @lru_cache |
@lru_cache with a list argument |
TypeError: unhashable type: 'list' |
cache keys must be hashable | pass a tuple |
| caching an impure function | stale answers | same input no longer means same output | cache only pure functions |
Two of them run for real:
def timed(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@timed()
def hello():
print("hi")
Output:
Traceback (most recent call last):
File "main.py", line 6, in <module>
@timed()
~~~~~^^
TypeError: timed() missing 1 required positional argument: 'func'
def add_item(item, cart=None):
if not cart:
cart = []
cart.append(item)
return cart
mine = []
add_item("milk", mine)
print(mine) # still empty: my list was replaced
Output:
[]
Your turn: a call counter (solution)
Question: write a decorator count_calls so that greet.calls says how many times greet ran.
Idea: a function is an object, so the wrapper can carry its own attribute wrapper.calls. Set it to 0 when decorating, add 1 on every call. Keep @wraps and return the result.
from functools import wraps
def count_calls(func):
@wraps(func)
def wrapper(*args, **kwargs):
wrapper.calls += 1
return func(*args, **kwargs)
wrapper.calls = 0
return wrapper
@count_calls
def greet(name):
return f"Hi {name}"
print(greet("Asha"), greet("Ravi"))
print(greet.calls)
print(greet.__name__)
Output:
Hi Asha Hi Ravi
2
greet
O(1) extra time per call and O(1) space.
Mini project: a slow-function report
Combine both ideas: a decorator that times every call, collects the times in a dictionary, and a report at the end. The report is sorted slowest first.
import time
from functools import wraps
timings = {}
def timed(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
timings.setdefault(func.__name__, []).append(time.perf_counter() - start)
return result
return wrapper
@timed
def make_chai():
time.sleep(0.05)
@timed
def print_receipt():
time.sleep(0.12)
@timed
def total_bill(prices):
return sum(prices)
for _ in range(3):
make_chai()
total_bill([15, 20, 25])
print_receipt()
for name, ts in sorted(timings.items(), key=lambda kv: -sum(kv[1])):
print(f"{name:14} calls={len(ts)} total={sum(ts):.2f} s")
Output:
make_chai calls=3 total=0.15 s
print_receipt calls=1 total=0.12 s
total_bill calls=3 total=0.00 s
The exact times change a little each run. make_chai is the slowest in total (3 calls × 0.05 s), even though one receipt (0.12 s) is the slowest single call: a report like this tells you where the time really goes.
Practice questions (with solutions)
1. Write @retry(times=3): call the function again if it raises an exception, up to times tries.
Solution
A decorator with arguments needs three levels: retry(times) returns the decorator, which returns the wrapper. Re-raise the last error if every try fails.
from functools import wraps
def retry(times=3):
def deco(func):
@wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(1, times + 1):
try:
return func(*args, **kwargs)
except Exception as e:
print(f" try {attempt} failed: {e}")
if attempt == times:
raise
return wrapper
return deco
tries = 0
@retry(times=3)
def send_otp():
global tries
tries += 1
if tries < 3:
raise ConnectionError("network down")
return "sent"
print(send_otp())
Output:
try 1 failed: network down
try 2 failed: network down
sent
2. Count the paths in a grid: from the top-left to the bottom-right of an r × c grid, moving only right or down.
Solution
The number of paths to a cell is the paths from above plus the paths from the left. The same cells are asked many times, so memoize. O(r × c) time and space.
from functools import lru_cache
@lru_cache(maxsize=None)
def paths(r, c):
if r == 1 or c == 1:
return 1
return paths(r - 1, c) + paths(r, c - 1)
print(paths(3, 3))
print(paths(3, 7))
print(paths(18, 18))
print(paths.cache_info().misses)
Output:
6
28
2333606220
323
3. Climbing stairs: you climb 1 or 2 steps at a time. How many ways to climb n steps?
Solution
To reach step n, the last move came from step n − 1 or n − 2: ways(n) = ways(n - 1) + ways(n - 2), with ways(0) = 1 and ways(1) = 1. The same shape as Fibonacci.
from functools import cache
@cache
def ways(n):
if n < 2:
return 1
return ways(n - 1) + ways(n - 2)
print([ways(n) for n in range(1, 8)])
print(ways(50))
Output:
[1, 2, 3, 5, 8, 13, 21]
20365011074
4. Fix this function so each call gets a fresh dictionary.
def add_mark(name, mark, marks={}):
marks[name] = mark
return marks
Solution
def add_mark(name, mark, marks=None):
if marks is None:
marks = {}
marks[name] = mark
return marks
print(add_mark("Asha", 90))
print(add_mark("Ravi", 85))
Output:
{'Asha': 90}
{'Ravi': 85}
5. Write a decorator @log that prints the function's name, its arguments and its result.
Solution
repr() shows strings with quotes, so you can tell "5" from 5.
from functools import wraps
def log(func):
@wraps(func)
def wrapper(*args, **kwargs):
parts = [repr(a) for a in args] + [f"{k}={v!r}" for k, v in kwargs.items()]
result = func(*args, **kwargs)
print(f"{func.__name__}({', '.join(parts)}) -> {result!r}")
return result
return wrapper
@log
def total_bill(prices, tip=0):
return sum(prices) + tip
total_bill([40, 25], tip=5)
total_bill((10,))
Output:
total_bill([40, 25], tip=5) -> 70
total_bill((10,)) -> 10
Quick quiz
What does this print?
def f(x, seen=set()):
seen.add(x)
return len(seen)
print(f(1), f(2), f(1))
(A) 1 1 1 (B) 1 2 3 (C) 1 2 2 (D) 1 2 1
Answer
(C). The default set is made once and shared by every call. f(1) → {1}: 1. f(2) → {1, 2}: 2. f(1) again adds nothing new (a set keeps no duplicates): still 2.
def f(x, seen=set()):
seen.add(x)
return len(seen)
print(f(1), f(2), f(1))
Output:
1 2 2
Interview questions
- What's wrong with
def f(x, items=[])? The default list is created once, whendefruns, and shared by every call that leavesitemsout. Useitems=Noneand create the list inside. - When are default values evaluated? Once, at function definition time. They're stored in
f.__defaults__. - What is a decorator? A function that takes a function and returns a new function (usually a wrapper that adds behaviour before and after).
@decoabovedef fmeansf = deco(f). - Why
*args, **kwargsin a wrapper? So the wrapper accepts and passes on any arguments the decorated function takes. - What does
functools.wrapsdo? It copies the wrapped function's__name__,__doc__,__module__and more onto the wrapper (and sets__wrapped__), so introspection, logs andhelp()show the real function. - How do you write a decorator that takes arguments? An extra outer function:
def repeat(n): def deco(func): ... return deco.@repeat(3)callsrepeat(3)first. - What is memoization? Caching a function's results by its arguments so each distinct input is computed once. It turns naive Fibonacci from exponential to O(n).
- What does
lru_cacheneed from the arguments? They must be hashable, because they become a dictionary key. Lists and dicts aren't; tuples, strings and numbers are. - What does LRU mean? Least recently used: when the cache is full, the entry used longest ago is removed.
- When should you not memoize? When the function isn't pure (it depends on time, randomness, I/O or changing state), or when inputs rarely repeat and the cache would only waste memory.
Glossary
| Term | Meaning | Example |
|---|---|---|
| Parameter / argument | The name in the def line / the value passed in a call |
def f(x) / f(5) |
| Default argument | A value used when the caller leaves the argument out | def f(x, tip=0) |
__defaults__ |
Where a function stores its default values | add_item.__defaults__ |
| Mutable | Can be changed in place | list, dict, set |
| Immutable | Can't be changed in place | int, str, tuple, None |
is None |
Checks for the None object itself |
if cart is None: |
| Function object | A function is a value: it can be passed, returned, stored | twice(greet) |
| Inner function | A function defined inside another function | wrapper inside timed |
| Decorator | Takes a function, returns a new (wrapped) one | @timed |
@ syntax |
@deco above def f means f = deco(f) |
@lru_cache(maxsize=None) |
| Wrapper | The new function a decorator returns | def wrapper(*args, **kwargs) |
*args |
Collects positional arguments into a tuple | (40, 25) |
**kwargs |
Collects keyword arguments into a dict | {'tip': 10} |
| Positional / keyword argument | Passed by position / passed as name=value |
f(1) / f(tip=1) |
functools.wraps |
Copies the original's name and docstring onto the wrapper | @wraps(func) |
__name__ |
A function's name | total_bill.__name__ |
| Docstring | A function's help text, the first string in its body | """Add up prices.""" |
time.perf_counter() |
A clock for measuring short time spans, in seconds | start = time.perf_counter() |
time.sleep(s) |
Pauses for s seconds | time.sleep(0.3) |
| Recursion | A function calling itself | fib(n - 1) + fib(n - 2) |
global |
Lets a function assign to a module-level variable | global calls |
| Exponential time | Work grows like 2ⁿ: each +1 to n roughly doubles it | naive Fibonacci |
| Memoization | Remembering results by input so each is computed once | memo[n] = ... |
| Cache | Where remembered results are kept | the memo dict |
| Pure function | Same input → same output, no side effects | fib(n) |
functools.lru_cache |
A built-in memoizing decorator | @lru_cache(maxsize=None) |
functools.cache |
lru_cache(maxsize=None) (Python 3.9+) |
@cache |
| LRU | Least recently used: what a full cache throws out first | maxsize=128 |
| Hit / miss | Answer found in the cache / had to be computed | cache_info() |
| Hashable | Has a fixed hash value, can be a dict key or set item | tuples yes, lists no |
Summary
- Default values are made once, when
defruns. A mutable default ([],{},set()) is shared by every call. UseNoneand create the object inside. - Functions are values. A decorator takes a function and returns a wrapper;
@decomeansf = deco(f). - A correct wrapper accepts
*args, **kwargs, passes them on, keeps and returns the result, and uses@wraps(func). - Naive recursive Fibonacci makes over 7 million calls for
fib(32); a memo dictionary makes 63. Memoization turns exponential time into O(n) time with O(n) space. @lru_cachedoes it for you; its arguments must be hashable, and only pure functions should be cached.- Next episode: closures and generators: a counter factory with
nonlocal, the late-binding lambda trap, and your ownrange().