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 result of calling a function that has no `return` statement?
Answer: None
Python functions implicitly return `None` when execution reaches the end without a `return` statement.
How do you enforce that a function argument must be passed as a keyword-only argument?
Answer: Place it after a bare * or after *args in the signature
Parameters listed after `*` or `*args` in a function signature can only be passed by keyword, not positionally.
What does `**kwargs` capture in a function definition?
Answer: Extra keyword arguments as a dictionary
`**kwargs` collects any extra keyword arguments into a dictionary where keys are argument names.
In Python, what is a first-class function?
Answer: A function that can be passed as an argument, returned, or assigned to a variable
First-class functions are treated like any other object — they can be stored in variables, passed to other functions, and returned from functions.
What error does Python raise when you try to read a local variable before assigning it?
Answer: UnboundLocalError
`UnboundLocalError` is raised when a local variable is referenced before it has been assigned a value.
Which of the following correctly unpacks a list into positional arguments?
Answer: f(*my_list)
The `*` operator before an iterable in a function call unpacks its items as separate positional arguments.
What is the purpose of a docstring in a function?
Answer: It documents the function and is accessible via __doc__
A docstring is a string literal as the first statement of a function body; Python stores it in the function's `__doc__` attribute.