Stay on top of the latest thoughts, strategies and insights from enterprising peers.
How to retrieve source code of Python functions
Learn to use the inspect and dill libraries to access Python functions' source code.
296 readers like this
296 readers like this
Image by:
Opensource.com
Sometimes we want to know what some functions' source codes look like or where they are, or we need to manipulate the source codes as character strings. In such cases, we need to have a convenient way to retrieve our Python functions' source codes.
inspect is a built-in library. It's already there after you install Python on your computer. The inspect module provides several useful functions to help you get information about live objects, such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. Among its many features, its capability to retrieve the source code of functions stands out.
In IPython or Jupyter, we can also use this method to retrieve the source code of the functions that we defined in the console.
In [9]:
deftest(x):
return x*2
print(inspect.getsource(test))
def test(x): return x*2
In [10]:
print(inspect.getsourcefile(test))
<ipython-input-9-70ac3e17460c>
In [11]:
print(inspect.getsourcelines(test))
(['def test(x):\n', ' return x*2\n'], 1)
Note that retrieving source codes of self-defined functions only works in IPython or Jupyter. If we are using plain Python and define a function interactively, we will encounter error IOError: could not get source code and will not be able to retrieve the source code. This is because its setting only supports objects loaded from files, not interactive sessions.
dill
dillextends Python's pickle module for serializing and deserializing Python objects to the majority of the built-in Python types. At the same time, it can also retrieve the source code of your Python objects. Please note dill is not a standard library, so you must install it separately.
<type'str'> 195262 class DataFrame(NDFrame): """ Two-dimensional size-mutable, potentially heterogeneous tabular data
structure with labeled axes (rows and columns). Arithmetic operations
align on both row a
/Users/XD/anaconda/lib/python2.7/site-packages/pandas/core/frame.py
<type 'tuple'>
2
<type 'list'>
However, a big difference between dill and inspect is that dill's retrieving feature supports self-defined objects in the plain Python console.
From China and staying in Singapore now.
I majored in mathematics during both my bachelor and MSc studies. But then I found dealing with coding is more interesting. Thinking from engineering perspective really helps me a lot.
For more information, please refer to my webiste XD-DENG.com.
Comments are closed.