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
var2 = "Welcome to geeksforgeeks"
var3 = { "1" : "a" , "2" : "b" }
var4 = 25
var5 = [ 1 , 2 , 3 , 4 , 5 ]
var6 = ( 58 , 59 )
all_variables = dir ()
for name in all_variables:
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
not_my_data = set ( dir ())
var2 = "Welcome to geeksforgeeks"
var3 = { "1" : "a" , "2" : "b" }
var4 = 25
var5 = [ 1 , 2 , 3 , 4 , 5 ]
var6 = ( 58 , 59 )
my_data = set ( dir ()) - not_my_data
for name in 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
not_my_data = set ( globals ())
foo5 = "hii"
foo6 = 7
def func():
var2 = "Welcome to geeksforgeeks"
var3 = { "1" : "a" , "2" : "b" }
var4 = 25
var5 = [ 1 , 2 , 3 , 4 , 5 ]
var6 = ( 58 , 59 )
locals_stored = set ( locals ())
print ( "Printing Local Variables" )
for name in locals_stored:
val = eval (name)
print (name, "is" , type (val), "and is equal to " , val)
globals_stored = set ( globals ()) - not_my_data
print ( "\nPrinting Global Variables" )
for name in globals_stored:
if name ! = "not_my_data" and name ! = "func" :
val = eval (name)
print (name, "is" , type (val), "and is equal to " , val)
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