Master Python locals() and globals(): Dynamic Variable Tricks Explained
This article explores how Python's locals() and globals() functions reveal variable scopes, demonstrates why locals() cannot modify local variables, shows how globals() can dynamically alter global state, and combines them with exec() for powerful runtime variable creation and updates.
Once you master locals() and globals(), you can achieve dynamic variable modification, understand variable scope like a professional, and write more powerful, flexible code.
What are locals() and globals() ?
Python stores variables in different places based on where they are created.
locals() only shows variables within the current function or scope.
globals() shows all global‑level variables, including those defined outside functions.
Example output demonstrates that locals() displays only the variables inside my_func(), while globals() lists everything, including the function itself.
Difference between locals() and globals()
Below is a basic comparison of their behavior.
How locals() works inside a function
locals()captures only the variables created inside the function.
It returns a dictionary of local variables.
Can we modify locals() ?
Many think we can change local variables via locals(). The following example shows that modifying the returned dictionary does not affect the actual locals.
As discussed, locals() returns a dictionary, but changing it does not change the local variables. Python reads locals() inside functions but does not allow modification of those variables.
globals() – modifying everything in the global scope
globals()lets you modify variables, whereas locals() cannot change locals.
What happens when locals() is used outside a function?
Calling locals() at the global level behaves exactly like globals().
x = 50
print(locals() is globals()) # This prints TrueOutside a function, locals() and globals() refer to the same object.
When to use globals()
When you need to dynamically modify global variables.
When you want to create new global variables at runtime.
When dealing with dynamic imports or metaprogramming.
When to use locals()
When you want to inspect variables that exist inside a function.
When debugging and need to check the local scope.
locals() + exec() = Dynamic variable magic
Using exec() with locals() can create variables at runtime, but they only exist inside the function.
def create_var():
exec("new_var = 500")
print(locals()) # Does new_var exist?
create_var()The output shows the new variable inside the function, which disappears after the function returns.
globals() + exec() = Real dynamic variables
To create truly persistent variables, use globals() with exec().
exec("dynamic_var = 1000", globals())
print(dynamic_var) # 1000Now the variable remains in the global namespace.
globals() update() trick
If you need to create multiple variables at once, use globals().update().
globals().update({"a": 10, "b": 20, "c": 30})
print(a, b, c) # 10 20 30It is fast and dynamic.
Summary
locals()shows local variables (but cannot modify them). globals() shows and can modify global variables. exec() + globals() creates real dynamic variables. locals() works only inside functions. globals() can be used anywhere and lets you manipulate Python's variable space.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Mala Tang
Read source code together, write articles together, and enjoy spicy hot pot together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
