Python Decorators and Closures 2 — Questions and Answers
Question 1: What is a 'decorator factory' in Python?
- A built-in Python module for creating decorators
- A function that returns a decorator, allowing decorators to accept arguments (Correct answer)
- A class that automatically decorates all its methods
- A design pattern for organizing multiple decorators
Correct answer: A function that returns a decorator, allowing decorators to accept arguments
A decorator factory is a function that accepts arguments and returns a decorator, allowing you to create parameterized decorators.
Question 2: When multiple decorators are stacked, in what order are they applied to the function? @decorator_A @decorator_B def my_func(): pass
- decorator_A first, then decorator_B
- decorator_B first, then decorator_A (Correct answer)
- Both are applied simultaneously
- Order is determined randomly at runtime
Correct answer: decorator_B first, then decorator_A
Decorators are applied bottom-up, so decorator_B is applied first, then decorator_A wraps the result, equivalent to my_func = decorator_A(decorator_B(my_func)).
Question 3: What is the purpose of the nonlocal keyword in Python closures?
- To declare a variable as global
- To allow an inner function to modify a variable in the enclosing (non-global) scope (Correct answer)
- To prevent a closure from accessing outer variables
- To import variables from other modules
Correct answer: To allow an inner function to modify a variable in the enclosing (non-global) scope
nonlocal allows an inner function to modify (not just read) a variable defined in its enclosing but non-global scope.
Question 4: What is a 'free variable' in the context of Python closures?
- A variable that doesn't consume memory
- A variable used in a function but defined in an enclosing outer scope (Correct answer)
- A global variable accessible from any function
- A variable with no type annotation
Correct answer: A variable used in a function but defined in an enclosing outer scope
A free variable is a variable referenced inside a function but not defined in that function's local scope — it is bound in an enclosing scope and captured by the closure.
Question 5: What attribute of a Python function object holds the free variables captured by a closure?
- __closure__ (Correct answer)
- __freevars__
- __enclosed__
- __scope__
Correct answer: __closure__
The __closure__ attribute is a tuple of cell objects containing the values of variables captured by the closure, or None if the function captures no free variables.
Question 6: How many levels of function nesting are typically needed to create a parameterized decorator (a decorator that accepts its own arguments)?
- 1 level — just the decorator function itself
- 2 levels — the decorator and the wrapper
- 3 levels — outer accepts args, middle accepts func, inner wraps the call (Correct answer)
- 4 or more levels are always required
Correct answer: 3 levels — outer accepts args, middle accepts func, inner wraps the call
A parameterized decorator requires three levels: the outermost function accepts the decorator's arguments, the middle function accepts the target function, and the innermost function wraps the actual call.
Question 7: Can a decorator be applied to a class definition in Python?
- No, decorators only work with functions and methods
- Yes, class decorators receive the class as their argument and return a modified class (Correct answer)
- Only if the decorator is defined inside the class itself
- Only built-in decorators can be applied to classes
Correct answer: Yes, class decorators receive the class as their argument and return a modified class
Class decorators work the same way as function decorators — they receive the class as their argument and return a modified or replacement object.
What is a 'decorator factory' in Python?