0
0
Pythonprogramming~10 mins

Built-in scope in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Built-in scope
Start: Code runs
Look for variable in Local scope
Found?
NoLook in Enclosing scope
Found?
NoLook in Global scope
Use Local value
Found?
NoLook in Built-in scope
Use Enclosing
Use Built-in value
Variable not found: Error
When Python looks for a name, it checks local, then enclosing, then global, and finally built-in scope.
Execution Sample
Python
print(len([1, 2, 3]))

len = 5
print(len)

# print(len([1, 2, 3]))  # This would error now
This code shows how Python uses the built-in len function first, then a local variable named len, and how redefining len hides the built-in.
Execution Table
StepActionName looked upScope checkedResultOutput/Effect
1Call print(len([1, 2, 3]))lenLocal -> Enclosing -> Global -> Built-inBuilt-in len function foundPrints 3
2Assign len = 5lenLocallen variable created with value 5No output
3Call print(len)lenLocallen variable found with value 5Prints 5
4Call print(len([1, 2, 3]))lenLocallen variable found (int 5), not callableError if run (commented out)
💡 Execution stops after printing 5; calling len as function now errors because local len hides built-in.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
lenbuilt-in function5 (local variable shadows built-in)55
Key Moments - 2 Insights
Why does print(len([1, 2, 3])) work the first time but not after len = 5?
At step 1 in the execution_table, Python finds len in the built-in scope and uses the function. After step 2, len is a local variable (int 5), so at step 4 it no longer refers to the built-in function, causing an error.
Does assigning len = 5 delete the built-in len function?
No, the built-in len function still exists but is hidden by the local variable named len, as shown in variable_tracker where len changes from built-in to local.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of len?
AThe built-in len function
BThe integer 5
CAn error occurs
DUndefined
💡 Hint
Check the 'Result' column at step 3 in execution_table and variable_tracker for len value.
At which step does Python stop using the built-in len function?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'Scope checked' and 'Result' columns in execution_table for step 4.
If we rename the local variable from len to my_len, what happens at step 4?
APrints 3 using built-in len function
BError because len is undefined
CPrints the value of my_len
DPrints 5
💡 Hint
Consider how variable_tracker shows local variables shadowing built-ins and what happens if local len is removed.
Concept Snapshot
Built-in scope is the last place Python looks for names.
Python searches: Local -> Enclosing -> Global -> Built-in.
Built-in names like len exist unless shadowed by local/global variables.
Assigning a local variable with a built-in name hides the built-in.
Use caution when naming variables to avoid hiding built-ins.
Full Transcript
This visual execution shows how Python finds names using the built-in scope last. Initially, calling print(len([1, 2, 3])) uses the built-in len function and prints 3. Then, assigning len = 5 creates a local variable named len that hides the built-in. Printing len now shows 5. Trying to call len as a function after this causes an error because the local len is an integer, not a function. The variable tracker confirms len changes from built-in function to local integer. This teaches that built-in names exist but can be hidden by local variables with the same name, so naming variables carefully is important.