Local Scope#
Note
Source: Adapted from the C# edition (functions/localscope.rst) and
the SE4ML Python chapter (chapter_python.rst, lines 528–561 and
1972–2016). The LEGB scope rules and global statement are Python-
specific content from the SE4ML presentation.
Variables created inside a function are local to that function — they exist only while the function is running and are invisible outside it.
Local Variables#
Consider this function:
def compute_area(length, width):
area = length * width # 'area' is a local variable
return area
The variables length, width, and area are all local. After the
function returns, they are gone. Trying to access them from outside causes an
error:
>>> compute_area(5, 3)
15
>>> area
NameError: name 'area' is not defined
Why Local Scope?#
Local scope is a feature, not a limitation. It means:
Functions are self-contained. A function cannot accidentally modify variables in other parts of the program.
You can use the same name in different functions without conflict.
def add(a, b):
result = a + b # this 'result' is local to add()
return result
def multiply(a, b):
result = a * b # this 'result' is local to multiply()
return result
Both functions have a variable named result, but they are completely
independent.
How Python Looks Up Names#
When Python looks up a variable name inside a function, it searches in this order (LEGB):
Local — variables assigned in the current function.
Enclosing — local variables of any enclosing functions (for nested functions, covered later).
Global — variables assigned at the top level of the module.
Built-in — names built into Python (
print,len,range, etc.).
x = 10 # global
def show():
x = 99 # local — shadows the global
print(x)
show() # prints 99
print(x) # prints 10 — global unchanged
Using Global Variables (Avoid When Possible)#
If you assign to a name inside a function, Python treats it as local. If
you want to modify a global variable, use the global statement:
count = 0
def increment():
global count
count += 1
increment()
increment()
print(count) # 2
However, relying on global mutable state makes programs harder to understand and test. Prefer passing values as arguments and returning results.
Bad Scope Example#
Here is a common mistake:
def compute():
total = a + b # ERROR if a and b aren't defined somewhere above
If a and b are not local or global, Python raises a NameError.
The fix is to pass them as parameters:
def compute(a, b):
total = a + b
return total