How to use the dill module to retrieve source code In Python
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
How to retrieve source code from Python objects?
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.
Contact Us