Functions and Scope Flashcards
7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Functions and Scope flashcards as text
What does the `functools.wraps` decorator do?
Answer: Copies the wrapped function's metadata to the wrapper function
`functools.wraps` copies attributes like `__name__` and `__doc__` from the wrapped function to the decorator wrapper, preserving introspection.
What is the output of: `f = lambda x, y=2: x * y; print(f(3))`?
Answer: 6
`y` defaults to 2, so `f(3)` computes `3 * 2 = 6`.
Which built-in function applies a function to every item in an iterable?
Answer: map()
`map(func, iterable)` returns an iterator that applies `func` to each element of the iterable.
When is a Python function's default argument value evaluated?
Answer: At function definition time
Default values are evaluated once when the `def` statement is executed, not on each call — this is why mutable defaults can cause bugs.
What does `inspect.signature(func)` return?
Answer: A Signature object describing the function's parameters
`inspect.signature` returns a `Signature` object that contains information about the function's parameters and their defaults.
What is a generator function?
Answer: A function that uses `yield` to produce values lazily
A generator function contains at least one `yield` statement; calling it returns a generator iterator that produces values on demand.
What happens when you call `sorted([3,1,2], key=lambda x: -x)`?
Answer: [3, 2, 1]
Negating each element makes `sorted` order by descending value, so the result is `[3, 2, 1]`.