codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

27 / 68 topics
24Python Functions25Function Arguments (*args, **kwargs)26Python Lambda Functions27Python Namespace & Scope (Global/Local)28Python Closures29Python Recursion
Tutorials/Python Programming/Python Namespace & Scope (Global/Local)
🐍Python Programming

Python Namespace & Scope (Global/Local)

Updated 2026-05-15
20 min read

Python Namespace & Scope (Global/Local)

Understanding namespaces and scope is crucial for managing variables effectively in Python. This tutorial will delve into the different types of scopes—local, enclosing, global, and built-in—and explain how to use the global and nonlocal keywords.

Introduction

Namespaces are a fundamental concept in programming that help organize and manage variable names. In Python, each scope has its own namespace, which is essentially a dictionary mapping names (variable names) to objects (values). The scope determines where these variables can be accessed. Understanding the LEGB rule (Local, Enclosing, Global, Built-in) will help you navigate how Python resolves variable names.

Core Content

Local Scope

The local scope refers to the namespace of a function or method. Variables defined within a function are only accessible within that function and cannot be accessed from outside it.

local_scope.py
1def my_function():
2 local_var = 10
3 print(local_var)
4
5my_function()
6print(local_var) # This will raise an error
Output
10
Traceback (most recent call last):
File "local_scope.py", line 7, in <module>
  print(local_var)
NameError: name 'local_var' is not defined

Enclosing Scope

The enclosing scope refers to the namespace of any and all enclosing functions. If a variable is not found in the local scope, Python will look for it in the nearest enclosing scope.

enclosing_scope.py
1def outer_function():
2 enclosing_var = 20
3
4 def inner_function():
5 print(enclosing_var)
6
7 inner_function()
8
9outer_function()
Output
20

Global Scope

The global scope is the namespace of the current module. Variables defined at the top level of a script are in the global scope and can be accessed from any function within that module.

global_scope.py
1global_var = 30
2
3def my_function():
4 print(global_var)
5
6my_function()
Output
30

Built-in Scope

The built-in scope contains the names of all built-in functions and exceptions. These are always available, but they can be shadowed by variables in other scopes.

builtin_scope.py
1print = 40 # Shadowing the built-in print function
2
3def my_function():
4 print("Hello") # This will raise an error
5
6my_function()
Output
Traceback (most recent call last):
File "builtin_scope.py", line 6, in <module>
  my_function()
File "builtin_scope.py", line 4, in my_function
  print("Hello")
TypeError: 'int' object is not callable

The LEGB Rule

The LEGB rule summarizes the order in which Python searches for a variable:

  1. Local: Inside the current function.
  2. Enclosing: In any enclosing functions, from inner to outer.
  3. Global: In the global scope of the module.
  4. Built-in: In the built-in namespace.

The global Keyword

The global keyword allows you to modify a variable in the global scope from within a function.

global_keyword.py
1global_var = 50
2
3def my_function():
4 global global_var
5 global_var = 60
6
7my_function()
8print(global_var)
Output
60

The nonlocal Keyword

The nonlocal keyword allows you to modify a variable in the nearest enclosing scope from within a nested function.

nonlocal_keyword.py
1def outer_function():
2 enclosing_var = 70
3
4 def inner_function():
5 nonlocal enclosing_var
6 enclosing_var = 80
7
8 inner_function()
9 print(enclosing_var)
10
11outer_function()
Output
80

Practical Example

Let's create a practical example that demonstrates the use of local, global, and nonlocal scopes.

practical_example.py
1global_counter = 0
2
3def increment_global():
4 global global_counter
5 global_counter += 1
6 print(f"Global counter: {global_counter}")
7
8def outer_function():
9 enclosing_counter = 90
10
11 def inner_function():
12 nonlocal enclosing_counter
13 enclosing_counter += 10
14 print(f"Enclosing counter: {enclosing_counter}")
15
16 inner_function()
17 increment_global()
18
19outer_function()
Output
Enclosing counter: 100
Global counter: 1

Summary

  • Local Scope: Variables defined within a function.
  • Enclosing Scope: Variables in enclosing functions.
  • Global Scope: Variables at the top level of a module.
  • Built-in Scope: Built-in functions and exceptions.
  • LEGB Rule: Local, Enclosing, Global, Built-in.
  • global Keyword: Modify global variables within a function.
  • nonlocal Keyword: Modify enclosing variables within a nested function.

What's Next?

In the next tutorial, we will explore Python closures. Closures are functions that remember values in enclosing scopes even if they are not present in memory. Understanding closures will help you write more efficient and reusable code. Stay tuned!


PreviousPython Lambda FunctionsNext Python Closures

Recommended Gear

Python Lambda FunctionsPython Closures