← All Python Flashcard Decks

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
  1. 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.

  2. 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`.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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]`.