Skip to content
Related Articles

Related Articles

Viewing all defined variables in Python

View Discussion
Improve Article
Save Article
  • 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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!