Predict the Output: Variable Scope
Intermediate · Predict the output · from 15 Python Pitfalls That Trip Up Even Experienced Developers
From the lesson: 15 Python Pitfalls That Trip Up Even Experienced Developers
Python is famous for being readable and beginner-friendly. But lurking beneath that clean syntax are several traps that catch even experienced developers. These aren't bugs in Python - they're features that behave differently than you'd expect.
Read the lessonWhat does this code print? Pay attention to how Python resolves variable names.
x = "global"
def outer():
x = "outer"
def inner():
print(x)
inner()
outer()
print(x)x = "global"
def outer():
x = "outer"
def inner():
print(x)
inner()
outer()
print(x)
More Common Pitfalls exercises
- Fix the Mutable DefaultIntermediate
- Predict the Output: Late BindingIntermediate
- Safe List FilteringIntermediate
- Fix the Bug: Shallow Copy SurpriseIntermediate
- Safe Exception HandlingIntermediate
- Build a Safe Defaults FunctionIntermediate
All Python exercises · 50 practice problems · Advanced Python quiz