Open In App

How to get the list of all initialized objects and function definitions alive in Python?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to get the list of all initialized objects and function definitions that are alive in Python, so we are getting all those initialized objects details by using gc module we can get the details. GC stands for garbage collector which is issued to manage the objects in the memory, so from that module, we are using the get_objects() method

Syntax: gc.get_objects()

Example 1: Python program to get the details of initialized objects.

Here we will create some objects like a list, tuple, dict, and then get the alive initialize object using gc.

Python3




# import gc module
import gc
 
# create list object
a = [1,2,3,4,5]
print(a)
 
# create a variable
b = 12
print(b)
 
# create tuple object
c = (1,2,3,4,5)
print(c)
 
# create a  dictionary object
d = {'a':1, 'b':2}
print(d)
 
# get all the initialized objects
gc.get_objects()


Output:

Time Complexity: O(n)
Auxiliary Space: O(n)

Example 2: Python program to get the list of initialized functions

Python3




# import gc module
import gc
 
 
# define a function
def geek(name):
    print(name + " - Hello Geek!")
 
 
# call the function with names as
# parameter
geek("sravan")
geek("bobby")
geek("ojaswi")
geek("rohith")
geek("gnanesh")
 
# get all the initialized objects
gc.get_objects()


Output:



Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads