Last modified: May 29, 2026
Python Show All Variables Guide
When you write Python code, you often need to see all your variables. This is useful for debugging. It helps you understand what data your program holds.
Python gives you built-in tools to list variables. You can use functions like dir(), globals(), locals(), and vars(). Each tool works a bit differently.
This guide shows you how to use each one. You will see code examples and outputs. Let's start with the most common method.
Using dir() to List Variables
The dir() function is simple. It returns a list of names in the current scope. This includes variable names, function names, and module names.
It works without any arguments. When you call dir() with no arguments, it shows the current local scope.
Here is a basic example:
# Define some variables
name = "Alice"
age = 30
is_student = True
# Show all variables in the current scope
all_names = dir()
print(all_names)
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'age', 'is_student', 'name']
The output shows all names. Your variables name, age, and is_student appear at the end. The names starting with __ are Python internals. You can ignore them.
To see only your variables, filter the list. Use a list comprehension to remove names starting with underscores.
# Filter out Python internals
user_vars = [var for var in dir() if not var.startswith('_')]
print(user_vars)
['age', 'is_student', 'name']
Now you see only the variables you defined. This is very clean and readable.
Using globals() for Global Variables
The globals() function returns a dictionary. It contains all global variables. The keys are variable names. The values are the variable values.
This is great when you need both the name and the value. It works at the module level. It also works inside a function, but it still shows global variables.
Here is an example:
# Global variables
city = "New York"
population = 8336817
# Get global variables
global_vars = globals()
print(global_vars)
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': ..., '__spec__': None, '__builtins__': ..., 'city': 'New York', 'population': 8336817}
The output is a dictionary. You can access the value of a variable like this: global_vars['city']. This gives you 'New York'.
To see only your global variables, filter the dictionary. Remove keys that start with underscores.
# Filter for user-defined globals
user_globals = {k: v for k, v in globals().items() if not k.startswith('_')}
print(user_globals)
{'city': 'New York', 'population': 8336817}
This shows only your variables with their values. It is perfect for debugging.
Using locals() for Local Variables
The locals() function returns a dictionary of local variables. It works inside a function or a class. It shows variables that exist only in that scope.
This is very useful for checking function parameters and temporary variables. It helps you see what is happening inside your code.
Here is an example inside a function:
def calculate_area(radius):
pi = 3.14159
area = pi * radius ** 2
local_vars = locals()
print(local_vars)
calculate_area(5)
{'radius': 5, 'pi': 3.14159, 'area': 78.53975}
The output shows all local variables. It includes the parameter radius, the constant pi, and the result area. This is very helpful for debugging functions.
You can also use locals() outside a function. In that case, it returns the same as globals(). But it is best used inside a function.
Using vars() to Show Attributes
The vars() function is similar to dir(). It returns the __dict__ attribute of an object. For a module, class, or instance, it shows all attributes.
When you call vars() with no argument, it acts like locals(). It returns the local variables in the current scope.
Here is an example with a class:
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
my_car = Car("Toyota", "Camry", 2022)
car_attrs = vars(my_car)
print(car_attrs)
{'make': 'Toyota', 'model': 'Camry', 'year': 2022}
The output shows the attributes of the object. This is very useful for inspecting objects. It shows both the name and value.
For a module, vars() works like globals(). It returns the module's dictionary. This includes all global variables.
Comparing All Methods
Each method has its own use case. Here is a quick comparison:
dir(): Returns a list of names. Good for a quick overview.globals(): Returns a dictionary of global variables. Shows names and values.locals(): Returns a dictionary of local variables. Best inside functions.vars(): Returns attributes of an object. Useful for classes and modules.
Choose the one that fits your situation. For simple debugging, dir() is fast. For detailed inspection, use globals() or locals().
If you work with JavaScript, you can learn about JavaScript variables for comparison. Python and JavaScript handle variables differently. But both use similar concepts.
For more examples of variable handling, see our JavaScript variables examples. It shows how other languages manage data.
Practical Tips for Variable Inspection
Always filter out internal names. Use list comprehensions or dictionary comprehensions. This makes your output clean and readable.
Use pprint for large dictionaries. It formats the output nicely. This is very helpful when you have many variables.
Here is an example with pprint:
import pprint
# Many variables
a = 1
b = 2
c = 3
d = 4
# Use pprint for clean output
pprint.pprint(locals())
{'__builtins__': ,
'__doc__': None,
'__loader__': ,
'__name__': '__main__',
'__package__': None,
'__spec__': None,
'a': 1,
'b': 2,
'c': 3,
'd': 4,
'pprint': }
The output is sorted and easy to read. Use this for complex debugging sessions.
Conclusion
Python gives you powerful tools to show all variables. Use dir() for a list. Use globals() and locals() for dictionaries with values. Use vars() for object attributes.
These functions are essential for debugging. They help you understand your code. They also help you find bugs quickly.
Practice using each method. Filter out internal names. Use pprint for large outputs. Your debugging will become much easier.
For a deeper look at variable types, check our types of JavaScript variables article. It explains different data types in another language.
Remember, clean code starts with clear understanding. Knowing your variables is the first step.