What does nonlocal do in Python 3?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Python 3, the nonlocal
statement plays a critical role in managing variable scopes, particularly within nested functions. Understanding how nonlocal
operates is essential for programmers looking to write more efficient and pythonic code, especially when dealing with closures or nested functions. This article will delve into the specifics of how the nonlocal
statement functions, its differences compared to other scope-related statements like global
, and its intended use cases.
Understanding Scope in Python
Before getting into the nuances of the nonlocal
statement, it's important to understand the concept of scope in Python. Variables in Python can exist in different scopes, and Python generally has four types of variable scope:
- Local: Variables defined within a function and can only be accessed there.
- Enclosing: Variables present in the local scope of an enclosing function, applicable mainly to nested functions.
- Global: Variables defined at the top level of a module or declared with the
globalkeyword within a function. - Built-in: Variables that are pre-assigned in Python such as
True,False, etc.
The nonlocal
Statement
The nonlocal
statement is used to refer to variables in the nearest enclosing scope outside of the local scope that a function is defined in. This is particularly relevant when dealing with nested functions. With nonlocal
, you can modify or update variables within an enclosing scope, but it does not apply to global variables.
Syntax
The basic syntax for nonlocal
is as follows:
- You have nested functions.
- You need to modify a variable in an enclosing scope (not the global scope).
- You wish to maintain stateful data without using instance variables in a class.
nonlocal: Only affects variables defined in an enclosing (non-global) scope.global: Refers to variables in the module level, making it possible to read or overwrite global variables.- Nonexistent Enclosing Variables: Attempting to declare a nonlocal variable that doesn’t exist in the enclosing scope will raise a
SyntaxError. - Declarative: Just like
global,nonlocalmust be declared before it is used in an assignment within the function. - Performance: Using
nonlocalis a lightweight way to handle mutable state, especially for function-level encapsulation. - Alternative: If the use of
nonlocalfeels complex, especially for managing states, consider using closures or object-oriented approaches.
Related reading
- What does numpy.random.seed0 do?
- What does Pythonic mean?
- What does Python's eval do?
- What does random.seed do in Python?
- What does Ruby have that Python doesn''t, and vice versa?
- What does s mean in a Python format string?
- What does script xyz.py returned exit code 0 mean in Python?
- What does sklearn RidgeClassifier do?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.