Scope
Explain where names are found and pass dependencies through parameters.
On this page
The concept
Python resolves names through local, enclosing, global, and built-in scopes, often abbreviated LEGB. A local name exists within a function call. An enclosing name comes from a surrounding function. A module-level name is global to that module, not to every program.
Passing a tax rate as a parameter makes the dependency explicit. Changing global state inside a calculation makes tests and reuse harder. Prefer inputs and return values unless shared state is a deliberate part of the design.
Worked example
tax_rate = 0.1
def price_with_tax(price, rate):
tax = price * rate
return price + tax
def outer():
message = "From the enclosing function"
def inner():
return message
return inner()
print(price_with_tax(100, tax_rate))
print(outer())
print(tax_rate)Trace the result
- tax is local to price_with_tax and is used to construct its return value.
- The tax rate is supplied by the caller instead of silently read from unrelated state.
- inner reads message from its enclosing outer function, demonstrating a different scope level.
110.0
From the enclosing function
0.1Line-by-line walkthrough
Line 1: tax_rate = 0.1
Evaluate 0.1, then bind or update tax_rate with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Lines 4–6: def price_with_tax(price, rate):
Define the function price_with_tax with input names price, rate. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.
Line 5: tax = price * rate
Evaluate price * rate, then bind or update tax with the result. Assignment itself does not imply a deep copy.
Line 6: return price + tax
Evaluate price + tax and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.
Lines 9–15: def outer():
Define the function outer with input names no parameters. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.
Line 10: message = "From the enclosing function"
Evaluate 'From the enclosing function', then bind or update message with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Lines 12–13: def inner():
Define the function inner with input names no parameters. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.
Line 13: return message
Evaluate message and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.
Line 15: return inner()
Evaluate inner() and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.
Line 18: print(price_with_tax(100, tax_rate))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 19: print(outer())
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 20: print(tax_rate)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Assigning to a name inside a function makes it local unless declared otherwise. Reading it before that local assignment can raise UnboundLocalError.