Lesson 12 of 55
PythonScope and Namespace
20 minutes
Course progress22%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 12 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
Python follows the LEGB rule to resolve names: Local → Enclosing → Global → Built‑in. Understanding this helps you avoid accidentally shadowing variables.
count = 0 # Global
def outer():
count = 1 # Enclosing
def inner():
count = 2 # Local
print('inner count:', count)
inner()
print('outer count:', count)
outer()
print('global count:', count)
Running the script prints 2, 1, and 0, demonstrating each scope in action.