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 is the difference between `return` and `yield` in Python?
Answer: `return` exits the function and returns a value; `yield` pauses execution and produces a value
`return` terminates the function call, while `yield` suspends it and produces a value, resuming from the same point on the next iteration.
Which of the following is a positional-only parameter syntax introduced in Python 3.8?
Answer: def f(x, /, y):
The `/` separator in a parameter list marks all parameters before it as positional-only — they cannot be passed by keyword.
What does `functools.partial` do?
Answer: Creates a new callable with some arguments pre-filled
`functools.partial(func, *args, **kwargs)` returns a new callable with the given arguments pre-applied to `func`.
What is the scope of a variable defined inside a `for` loop in Python?
Answer: Function scope — it remains accessible after the loop
Python has no block scope; loop variables remain in the enclosing function (or module) scope after the loop finishes.
What does a decorator fundamentally do in Python?
Answer: Wraps a function to extend or alter its behavior without modifying its source
A decorator is a callable that takes a function, adds behavior, and returns a new callable — applied using the `@decorator` syntax.
What is the output of: `def outer(): x = 10; def inner(): print(x); inner(); outer()`?
Answer: 10
`inner` closes over `x` from `outer`'s enclosing scope and can read it, printing `10`.
Which statement correctly describes recursive functions in Python?
Answer: Python enforces a default recursion limit (typically 1000) and raises RecursionError if exceeded
Python sets a default recursion limit (usually 1000) accessible via `sys.getrecursionlimit()`; exceeding it raises `RecursionError`.