How to get the list of all initialized objects and function definitions alive in Python?
Last Updated :
20 Feb, 2023
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
a = [ 1 , 2 , 3 , 4 , 5 ]
print (a)
b = 12
print (b)
c = ( 1 , 2 , 3 , 4 , 5 )
print (c)
d = { 'a' : 1 , 'b' : 2 }
print (d)
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
def geek(name):
print (name + " - Hello Geek!" )
geek( "sravan" )
geek( "bobby" )
geek( "ojaswi" )
geek( "rohith" )
geek( "gnanesh" )
gc.get_objects()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...