Determine if variable is defined in Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python does not have a special operator that asks whether a local name exists before evaluation. If you reference a missing variable, the interpreter raises NameError, so the right technique depends on whether you want to use the variable, inspect a namespace, or redesign the code to avoid dynamic name checks entirely.
The Most Pythonic Check Is Often try And except
If your next step is to use the value, the cleanest approach is usually to try the access and catch NameError. This follows Python's "easier to ask forgiveness than permission" style.
This works because the interpreter resolves the name at runtime. If it cannot find threshold in the relevant scope chain, it raises NameError.
This approach is especially good when the variable may or may not exist and you already need to use it immediately. It avoids a separate existence test followed by a second lookup.
Inspecting Namespaces With locals() And globals()
If you only need to know whether a name is present, inspect the appropriate namespace dictionary. locals() returns the current local namespace, while globals() returns the module-level namespace.
Use these functions when you are explicitly working with namespaces, such as in metaprogramming, simple template engines, or debugging utilities. They are less attractive for ordinary application logic because code becomes harder to read when it relies on variable names as strings.
Prefer Explicit Defaults When You Control The Design
Many "is this variable defined" questions are really design questions. If your code depends on an optional value, explicit initialization is usually clearer than probing for a missing name.
This style is easier to test because the function's inputs are visible in its signature. It also prevents surprises caused by variables appearing in outer scopes.
Another common case is reading configuration from the environment. In that situation, look up the key from os.environ instead of checking for a Python variable:
The more explicit your source of truth is, the less often you need dynamic existence checks.
Scope Matters
Python resolves names in local, enclosing, global, and built-in scopes. That means the answer can change depending on where you ask the question.
Inside inner, the name message is usable because Python finds it in the enclosing scope, even though "message" in locals() is False. This is why a simple namespace membership test does not always tell the whole story.
If your goal is "can I use this name here," then try and except NameError is more accurate than only checking locals().
Common Pitfalls
- Writing
if my_var:to test existence. That still evaluatesmy_var, so it raisesNameErrorif the name is missing. - Catching a broad
Exceptionwhen you only mean to handleNameError. That can hide unrelated bugs inside thetryblock. - Assuming
locals()tells you everything about outer scopes. It only reports the current local namespace. - Using dynamic name checks when a normal default value or function parameter would be clearer.
- Confusing a missing variable with a variable whose value is
None. Those are different states and should usually be handled differently.
Summary
- If you need to use a possibly missing variable,
tryandexcept NameErroris usually the clearest option. - Use
locals()orglobals()only when you intentionally need namespace inspection. - Prefer explicit initialization and function parameters over dynamic name probing when you control the code design.
- Remember that Python name lookup depends on scope, not only on the current local dictionary.

