How to retrieve source code from Python objects?
Last Updated :
15 Sep, 2022
We are given a object and our task is to retrieve its source code, for this we have inspect module, dill module and dis module built-in standard libraries in Python programming. They provide several useful functions to track information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. getsource() method is used to get the source code of Python objects.
Syntax: inspect.getsource(object)
Parameter: The object type parameter, of which we want the source code.
Return type: text of the source code for an object
Note: An IOError is raised if the source code cannot be retrieved.
Using the inspect module to retrieve source code
Example: Here we are importing the inspect module first and then using the inspect.getsource() function to fetch the source code of the given object.
python3
# import inspect library
import inspect
def test(x):
return x * 2
print(inspect.getsource(test))
Output:
def test(x):
return (x+2)*(x-2)
Example: Here we are fetching the source code for the given object.
python3
# import inspect library
import inspect
def far(n):
factorial = 1
if int(n) >= 1:
for i in range (1, int(n)+1):
factorial = factorial * i
return factorial
source = inspect.getsource(far)
print(source)
Output:
def far(n):
factorial = 1
if int(n) >= 1:
for i in range (1, int(n)+1):
factorial = factorial * i
return factorial
Example: We can use inspect on built in library functions and objects also.
python3
# import inspect library
import inspect
import pandas
source_DF = inspect.getsource(pandas.DataFrame)
print(source_DF[:100])
Output:
class DataFrame(NDFrame):
"""
Two-dimensional size-mutable, potentially heterogeneous tabula
Using the dis module to retrieve source code
Here we are using the dis module in Python to fetch the source code of the specified object.
Python3
import dis
def fun(a1, a2):
# do something with args
a = a1 + a2
return a
dis.dis(fun)
Output:
50 0 LOAD_FAST 0 (a1)
2 LOAD_FAST 1 (a2)
4 BINARY_ADD
6 STORE_FAST 2 (a)
51 8 LOAD_FAST 2 (a)
10 RETURN_VALUE
Using the dill module to retrieve source code
Here we are using the dill module in Python to fetch the source code of the specified object.
Python3
import dill
def fun(a1,a2):
a = a1 + a2
return a
source=dill.source.getsource(fun)
print(source[:200])
Output:
def fun(a1,a2):
a = a1 + a2
return a
Similar Reads
How to read a JSON response from a link in Python? There is a huge amount of data available on the web and most of them are in form of (JavaScript Object Notation) JSON. But it is difficult for humans to directly read and use it. To resolve this problem in python we have different libraries which help us to read the JSON data fetched from the web. T
2 min read
Version Control for Python Projects: Why and How to Use Git In todayâs fast-paced software development environment, managing changes to code efficiently is crucial. Version control systems (VCS) are essential tools for developers, enabling collaboration, tracking changes, and maintaining a history of project modifications. This article delves into the import
5 min read
How to get value from address in Python ? In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like
4 min read
How to get the memory address of an object in Python In Python, everything is an object, from variables to lists and dictionaries everything is treated as objects. In this article, we are going to get the memory address of an object in Python. Method 1: Using id() We can get an address using the id() function. id() function gives the address of the pa
2 min read
Python | page_source method in Selenium page_source method is used retrieve the page source of the webpage the user is currently accessing. The main use of this method to find something in the page source like finding any data or keyword. Page source : The source code/page source is the programming behind any webpage. Syntax : driver.page
1 min read
Where Does Python Look for Modules? Modules are simply a python .py file from which we can use functions, classes, variables in another file. To use these things in another file we need to first import that module in that file and then we can use them. Modules can exist in various directories. In this article, we will discuss where do
3 min read