Viewing all defined variables in Python
Last Updated :
11 Dec, 2020
In this article, we are going to discuss how to view all defined variables in Python. Viewing all defined variables plays a major role while debugging the code.
Method 1: Using dir() function
dir() is a built-in function to store all the variables inside a program along with the built-in variable functions and methods. It creates a list of all declared and built-in variables. There are two different ways to view all defined variables using dir( ). They are discussed below.
When no user-defined variable starts with '__' :
- Define some variables of various types that are not starting with '__'
- Call dir and store it in a variable. It stores all the variable names defined before in the form of a list and stores the variable names as a string.
- Iterate over the whole list where dir( ) is stored.
- Print the item if it doesn't start with '__'
Example:
Python3
# Define some variables of various types
# that are not starting with '__'
var2 = "Welcome to geeksforgeeks"
var3 = {"1": "a", "2": "b"}
var4 = 25
var5 = [1, 2, 3, 4, 5]
var6 = (58, 59)
# call dir and store it in a variable.
# It stores all the variable names defined
# before in the form of a list
# and stores the variable names as a string.
all_variables = dir()
# Iterate over the whole list where dir( )
# is stored.
for name in all_variables:
# Print the item if it doesn't start with '__'
if not name.startswith('__'):
myvalue = eval(name)
print(name, "is", type(myvalue), "and is equal to ", myvalue)
Output:
var2 is <class 'str'> and is equal to Welcome to geeksforgeeks
var3 is <class 'dict'> and is equal to {'1': 'a', '2': 'b'}
var4 is <class 'int'> and is equal to 25
var5 is <class 'list'> and is equal to [1, 2, 3, 4, 5]
var6 is <class 'tuple'> and is equal to (58, 59)
Storing the built-in variables and ignoring them
- Create a new variable and store all built-in functions within it using dir( ).
- Define some variables of various types.
- Again call dir and store it in a list subtracting the built-in variables stored previously.
- Iterate over the whole list.
- Print the desired items
Example:
Python3
# Create a new variable and store all
# built-in functions within it using dir( ).
not_my_data = set(dir())
# Define some variables of various types.
var2 = "Welcome to geeksforgeeks"
var3 = {"1": "a", "2": "b"}
var4 = 25
var5 = [1, 2, 3, 4, 5]
var6 = (58, 59)
# Again call dir and store it in a list
# subtracting the built-in variables stored
# previously.
my_data = set(dir()) - not_my_data
# Iterate over the whole list is stored.
for name in my_data:
# Exclude the un-necessary variable named not_my_data
if name != "not_my_data":
val = eval(name)
print(name, "is", type(val), "and is equal to ", val)
Output:
var2 is <class 'str'> and is equal to Welcome to geeksforgeeks
var3 is <class 'dict'> and is equal to {'1': 'a', '2': 'b'}
var6 is <class 'tuple'> and is equal to (58, 59)
var4 is <class 'int'> and is equal to 25
var5 is <class 'list'> and is equal to [1, 2, 3, 4, 5]
Method 2: To print local and global variables
Locals() is a built-in function that returns a list of all local variables in that particular scope. And globals() does the same with the global variables.
Approach
- Create a list of all global variables using globals( ) function, to store the built-in global variables.
- Declare some global variables
- Declare a function.
- Declare some local variables inside it.
- Store all the local variables in a list, using locals keyword.
- Iterate over the list and print the local variables.
- Store the global variables in a list using globals keyword and subtract the previously created list of built-in global variables from it.
- Print them.
- Call the function.
Example:
Python3
# Create a list of all global variables using
# globals( ) function, To store the built-in
# global variables.
not_my_data = set(globals())
# Declare some global variables
foo5 = "hii"
foo6 = 7
# Declare a function.
def func():
# Declare some local variables inside it.
var2 = "Welcome to geeksforgeeks"
var3 = {"1": "a", "2": "b"}
var4 = 25
var5 = [1, 2, 3, 4, 5]
var6 = (58, 59)
# Store all the local variables in a list,
# using locals keyword.
locals_stored = set(locals())
# Iterate over the list and print the local
# variables.
print("Printing Local Variables")
for name in locals_stored:
val = eval(name)
print(name, "is", type(val), "and is equal to ", val)
# Store the global variables in a list using
# globals keyword and subtract the previously
# created list of built-in global variables from it.
globals_stored = set(globals())-not_my_data
# Print the global variables
print("\nPrinting Global Variables")
for name in globals_stored:
# Excluding func and not_my_data as they are
# also considered as a global variable
if name != "not_my_data" and name != "func":
val = eval(name)
print(name, "is", type(val), "and is equal to ", val)
# Call the function.
func()
Output:
Printing Local Variables
var2 is <class 'str'> and is equal to Welcome to geeksforgeeks
var6 is <class 'tuple'> and is equal to (58, 59)
var4 is <class 'int'> and is equal to 25
var5 is <class 'list'> and is equal to [1, 2, 3, 4, 5]
var3 is <class 'dict'> and is equal to {'1': 'a', '2': 'b'}
Printing Global Variables
foo6 is <class 'int'> and is equal to 7
foo5 is <class 'str'> and is equal to hii
Similar Reads
Context Variables in Python Context variable objects in Python is an interesting type of variable which returns the value of variable according to the context. It may have multiple values according to context in single thread or execution. The ContextVar class present in contextvars module, which is used to declare and work wi
4 min read
Global and Local Variables in Python In Python, global variables are declared outside any function and can be accessed anywhere in the program, including inside functions. On the other hand, local variables are created within a function and are only accessible during that functionâs execution. This means local variables exist only insi
7 min read
Assign Function to a Variable in Python In Python, functions are first-class objects, meaning they can be assigned to variables, passed as arguments and returned from other functions. Assigning a function to a variable enables function calls using the variable name, enhancing reusability.Example:Python# defining a function def a(): print(
3 min read
How To Print A Variable's Name In Python In Python, printing a variable's name can be a useful debugging technique or a way to enhance code readability. While the language itself does not provide a built-in method to directly obtain a variable's name, there are several creative ways to achieve this. In this article, we'll explore five simp
3 min read
Get Variable Name As String In Python Getting a variable name as a string in Python means finding the label that points to a specific value. For example, if x = "Python", we can loop through globals() or locals() and compare values using the is operator to find that the name x points to "Python". Letâs explore some simple and effective
2 min read
Protected variable in Python Prerequisites: Underscore ( _ ) in Python A Variable is an identifier that we assign to a memory location which is used to hold values in a computer program. Variables are named locations of storage in the program. Based on access specification, variables can be public, protected and private in a cl
2 min read