๐ Step 1: What is __globals__?
-
Every function in Python has a __globals__ attribute.
-
It’s a dictionary containing the global namespace where the function was defined.
-
That dictionary also contains a key called "__builtins__".
๐ Step 2: What is __builtins__?
"__builtins__" points to Python’s built-in functions and objects.
-
Depending on the environment:
-
Sometimes it’s a dictionary of builtins.
-
Sometimes it’s the builtins module.
-
So here:
add.__globals__['__builtins__']๐ is the builtins module.
๐ Step 3: Accessing sum
add.__globals__['__builtins__'].sum๐ fetches the sum function from the builtins module.
๐ Step 4: What is range(3)?
range(3) → [0, 1, 2] # when iterated๐ Step 5: Calling it
sum(range(3)) = 0 + 1 + 2 = 3✅ Final Output:
⚡ Why not just sum(range(3))?
-
Normally, you’d just use sum(range(3)).
-
Using add.__globals__['__builtins__'].sum is a deep lookup:
-
function → global namespace → builtins module → sum function.
-
-
It shows that Python lets you reach built-in functions through namespaces manually.
.png)

0 Comments:
Post a Comment