Functions & Functional Patterns Interview Questions (Python)
10 Functions & Functional Patterns questions from the Python track. Read each short answer here, then open a question for the full explanation, examples, common mistakes, and sources.
- Mid-level
What is the difference between positional-only and keyword-only parameters?
Parameters before the / marker cannot be passed by name (positional-only), and parameters after * must be passed by name (keyword-only).
Read the full explanation - Junior
Why is a list as a default parameter value a problem?
Because the default value is evaluated only once, when the function is defined, so all calls share the same list and data accumulates in it.
Read the full explanation - Junior
How do *args and **kwargs work?
*args collects extra positional arguments into a tuple and **kwargs collects extra keyword arguments into a dict; * and ** can also be used at call time to unpack values.
Read the full explanation - Mid-level
What is a closure, and what is the late-binding problem inside loops?
A closure is an inner function that keeps a reference to variables of the enclosing scope, but the variable's value is read at call time, not creation time, so all functions created in a loop see the loop variable's last value.
Read the full explanation - Mid-level
How do you write a decorator that preserves the original function's metadata?
Write a function that receives the original function and returns a wrapper, and apply functools.wraps to the wrapper to copy __name__, __doc__, __module__ and __wrapped__.
Read the full explanation - Junior
Explain the LEGB rule for name resolution.
Python looks up a name in this order: Local (the current function), Enclosing (the surrounding functions), Global (the module), then Builtins.
Read the full explanation - Junior
When should you use lambda and when def?
Use lambda for a small expression passed directly as an argument (such as key in sorted), and use def for any logic that needs a name, more than one expression, documentation or tests.
Read the full explanation - Mid-level
What is functools.partial for?
It creates a new callable that fixes some arguments of an existing function, which helps when passing a callback that needs fixed context.
Read the full explanation - Mid-level
What are the limits of recursion in Python, and when should you choose a loop?
Every call uses a stack frame, the default recursion depth limit is about 1000, and Python does not optimize tail calls, so a loop or an explicit stack suits deep data better.
Read the full explanation - Junior
What does it mean that functions in Python are first-class objects?
A function can be assigned to a variable, passed as an argument, returned from another function, and stored in data structures like any other object.
Read the full explanation