</>
Vizly

Python Functions, *args/**kwargs & Scope

July 3, 20263 min
PythonFundamentalsPracticeBeginner

Chapter 5 of Python Fundamentals. 14 practice drills on functions: *args/**kwargs, mutable default arguments, mutate vs rebind, scope, and lambdas. Predict the output before you run anything.

The rules, again

Same game as chapter 1: read the question, commit to a prediction, run it, and only then open the answer. Wrong prediction means the drill goes on your redo list for three days from now.

This chapter contains the single most famous gotcha in the language, the mutable default argument. It also contains the idea that unlocks half the series: functions are objects, defaults are evaluated once, and the mutate-vs-rebind distinction from chapter 1 decides whether the caller sees your changes.

Learn the concepts first

Rusty on the basics? Work through Functions and Multiple Function Arguments on learnpython.org first, then come back and drill.


One idea before you start

Two drills in this chapter crash on scope, so know the lookup order going in.

Definition

When Python sees a name, it searches four layers in order: Local (inside the current function), Enclosing (any outer function), Global (the module), then Built-in (things like len and print). Called the LEGB rule. The twist: assigning to a name anywhere in a function makes it local for the whole function, decided before a single line runs.


The drills

Click a question to open it. The answer stays hidden until you ask for it. Predict first.

14 drills. Predict first, then open the answer

Want more drills?

When these stop surprising you, let an AI generate fresh ones. Copy this prompt into ChatGPT, Claude, or any assistant you like:

You are my Python drill coach. Quiz me on: functions, *args and
**kwargs, argument unpacking, keyword-only arguments, default
argument evaluation, mutate vs rebind on parameters, LEGB scope,
lambdas, and functions as first-class objects.
 
Rules:
- Ask ONE question at a time. Show a short code snippet and ask me to
  predict the output (or the error) before running it.
- Wait for my answer. Do not reveal anything until I reply.
- If I am right, say so in one line and raise the difficulty slightly.
- If I am wrong, explain the why in 2-3 sentences, then give me a
  similar question later to check I really got it.
- Prefer tricky edge cases over textbook questions: def f(x, y=[]),
  defaults calling datetime.now(), count += 1 on a global,
  def f(a, *, b), rect(*size, **style), shadowing built-ins like list.
- Keep score. After 10 questions, list the concepts I missed so I can
  add them to my redo list.
 
Start with a medium question.

The predict-first rule matters more than the tool. Whatever generates the questions, always commit to an answer before you look.


Done?

Score yourself honestly and queue up your misses. Drills 9 and 10 are the pair to sit with: same-looking code, opposite outcomes, and the mutate-vs-rebind distinction between them is what interviewers are really probing when they ask "is Python pass-by-value or pass-by-reference?" (Answer: neither, it passes object references by value.)

Next chapter: comprehensions and generators, where one character of difference decides whether your data fits in memory.

Edit this page on GitHubโ†—