Python Closures & Scope
Test your understanding with multiple-choice questions based on what you just learned.
(Note: While the complete text of the "Tutorial on Python Closures & Scope" isn't actually fully available in the provided materials this quiz has simply been built directly from a deep-dive concepts covering Python's scope, closures, and the LEGB rule provided in the available excerpts.)
Practice Quiz: Python Closures & Scope
Question 1: According to Python's variable resolution sequence, what does simply an acronym "LEGB" stand for? A) Local, External, Global Base B) Local, Enclosing, Global, Built-in C) Logical Exception Global, Built-in D) Lambda, Enclosed, Generated, Bound
Correct Answer: B Explanation: LEGB stands for Local Enclosing, Global and Built-in, and this rule outlines the exact sequence the Python interpreter follows when resolving the names of variables in your code.
Question 2: What's the primary function of the LEGB rule in Python?
THE) To define the order in which the interpreter searches through distinct scopes towards resolve variable names.
B) To dictate the order into which exception handling blocks (try, except, else, finally) are executed.
C) Towards find out memory allocation priority for globally defined classes.
D) To strictly enforce that all nested functions use closure decorators.
Correct Answer: A Explanation: Python sort out names using LEGB rule, which defines the order in which an interpreter searches through distinct scopes for figure out what a variable name refers to.
Question 3: If you're basically working inside an inner nested function and need to modify a variable that belongs to the outer function, which keyword must you use?
A) global
B) super
C) enclosing
D) nonlocal
Correct Answer: D
Explanation: nonlocal keyword is specifically used to modify a variable on the enclosing (non-global) scope allowing an inner function to safely alter a variable from its outer function.
Question 4: On context of nested functions and scope, how is the "enclosing" (or nonlocal) scope defined? A) It's the built-in scope that contains Python's core functions, and b) It's the global namespace defined at a very top of the Python script. C) It is actually the region strictly inside the inner function itself. D) It is actually region inside the outer function of a nested function.
Correct Answer: D Explanation: The nonlocal or enclosed scope specifically refers to the region inside the outer function of nested function. This is the area that wraps inner function, allowing the inner function to access its state.
Question 5: Following a LEGB sequence, which scope will the Python interpreter always search first when attempting to resolve a variable name? A) Local Scope B) Enclosing Scope C) Global Scope D) Built-in Scope
Correct Answer: A Explanation: Python always checks the Local Scope (L) first. If it can't find a variable name there, it moves progressively outward towards an Enclosing scope then a Global scope, and finally a Built-in scope.