Scope (LEGB Rule) and Lambda Functions
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Variable visibility in Python is governed by namespaces and scope rules. For small throwaway operations, Python also supports anonymous inline lambda functions.
The LEGB scope rule
Scope is the part of a program where a variable name can be seen and used. A variable made inside a function is local and exists only while that call runs.
When resolving a variable name, Python searches four scopes in order: Local, Enclosing, Global, and Built-in.
x = "global"def outer(): x = "enclosing" def inner(): x = "local" print(x) inner()outer()Example 1 · A local name hides the outer one
rate = 18
def show_rate(): rate = 5 print(f"Inside: {rate}")
show_rate()print(f"Outside: {rate}")Inside: 5 Outside: 18
What this code does
- Assigning
rate = 5inside the function creates a new local variable. - The global
rateis untouched, so the last line still prints 18.
Python finds x = "local" in the Local scope first and prints local. If local were missing, it would search enclosing, then global, then built-in.
The global keyword
global keyword tells Python that a name inside a function means the module-level variable, so assigning to it changes the outer value.
Assigning to a variable inside a function makes it local by default. To modify a module-level global variable from inside a function, declare it with global.
counter = 0def increment(): global counter counter += 1
increment()print(counter)Example 2 · Changing a global on purpose
orders = 0
def place_order(): global orders orders += 1
place_order()place_order()place_order()print(orders)3
What this code does
global ordersmakes the assignment change the module-level variable.- Three calls add 1 three times, so the output is 3.
The console displays 1. In enterprise applications, minimize mutable global state to prevent hidden side effects.
Anonymous lambda functions
Lambda function is a small unnamed function written in one line as lambda arguments: expression. It returns the value of that expression.
A lambda function is a small, anonymous function defined with lambda arguments: expression. It can take multiple arguments but contains only a single expression.
double = lambda x: x * 2print(double(7))Example 3 · Pass a lambda to your own function
def apply_rule(price, rule): return rule(price)
print(apply_rule(200, lambda p: p - p * 10 // 100))print(apply_rule(200, lambda p: p + 50))180 250
What this code does
ruleis a parameter that receives a function, andrule(price)calls it.- Each lambda is a short rule written on the spot: 10% off gives 180, adding 50 gives 250.
Example 4 · Lambda used on the spot
print((lambda price: price + price * 18 // 100)(200))print(max(6, -9, 4, key=lambda n: abs(n)))236 -9
What this code does
- The first line builds a lambda and calls it at once with 200: 200 + 36 = 236.
maxuses the lambda as itskey, so it compares absolute values and picks -9.
Lambdas are often passed to another function as a short rule, such as key=lambda n: abs(n) for max(). Sorting a list with a key comes later, in Lists (opens in a new tab).
Next step
Check your understanding
9 quick questions