4.1.1.13–4.1.1.14 - Local and global variable scope (Python)
Learn which variables a subroutine can access, how long its local variables last, and how parameters and return values make changes to state easier to understand.
Lifetime and visibility
Two variables can have the same name without being the same variable, because their names belong to different scopes.
Local variable
A variable belonging to an invocation of a subroutine. It is accessible within that subroutine and its local binding normally exists only while that invocation executes.
Scope describes where a name is accessible. Lifetime describes when its binding exists. A function's parameters are local names too. When an ordinary call returns, its local namespace is no longer available to the caller; a returned object can still exist. Do not confuse the lifetime of a local name with the lifetime of every object that name referred to. In Python, an if or for block inside a function does not create another function-local scope.
Two meanings of total
{"code":"total = 100\n\ndef add_fee(amount):\n total = amount + 2\n print(total)\n return total\n\nresult = add_fee(7)\nprint(total)\nprint(result)\n","instructions":"Predict the three output lines and run the program. Change the local calculation to amount + 5, then run again. Add result2 = add_fee(1) followed by print(total). Explain why the module-level total still does not change.","files":[],"original_filename":"main.py","runtime_profile":"python-console-v1","file_mode":"fresh"}
Originally, the lines are 9, 100, 9. After changing the fee and adding the second call, they are 12, 100, 12, 6, 100. The function's total is local; assigning it does not alter the module-level total. Each call has its own amount and total. The returned number is stored in result or result2; those caller names are separate bindings.
Local state and side effects
Local variables let a routine do its work without accidentally changing another routine's working state.
A global variable is defined at module level in these Python examples. Functions in that module can read it if a local name does not hide it. It lasts while the module is in use. Assigning a name inside a function normally makes it local. To rebind a module-level variable from inside a function, Python requires global.
visits = 0
def record_visit():
global visits
visits += 1
record_visit()
record_visit()
print(visits)
This prints 2. Removing global visits does not create a working local counter: visits += 1 tries to read the uninitialised local visits, causing UnboundLocalError. A function can read a global constant such as MAX_SCORE = 100 without declaring global, provided it does not assign to that name.
A side effect is a change beyond the returned result, such as modifying global state, mutating a supplied object or writing a file. Globals may be appropriate for deliberately shared state, but many writers can make their value depend on call order. Local variables reduce name collisions and unintended interference. Parameters and returns expose dependencies, allowing a function to be tested with different data without resetting hidden globals. Local scope alone does not prevent mutation of a list passed as an argument.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account