Learn to use the inspect and dill libraries to access Python functions' source code.
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.
There are two Python libraries that may help:
inspect
is a built-in standard library
dill
is a third-party library
inspect
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 [1]: |
import pandas
import inspect
|
In [3]: |
source_DF = inspect.getsource(pandas.DataFrame)
print(type(source_DF))
<<class 'str'>>
|
In [4]: |
print(len(source_DF))
218432
|
In [5]: |
print(source_DF[:200])
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
|
In [6]: |
source_file_DF = inspect.getsourcefile(pandas.DataFrame)
print(source_file_DF)
D:\Users\dengdong\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py
|
In [7]: |
sourcelines_DF = inspect.getsourcelines(pandas.DataFrame)
print(type(sourcelines_DF))
print(len(sourcelines_DF))
print(type(sourcelines_DF[0]))
<class 'tuple'>
2
<class 'list'> |
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]: |
def test(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
dill
extends 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.
Its API is quite similar to inspect
's.
In [6]: |
import dill
source_DF = dill.source.getsource(pandas.DataFrame)
print(type(source_DF))
print(len(source_DF))
print(source_DF[:200])
source_file_DF = dill.source.getsourcefile(pandas.DataFrame)
print(source_file_DF)
sourcelines_DF = dill.source.getsourcelines(pandas.DataFrame)
print(type(sourcelines_DF))
print(len(sourcelines_DF))
print(type(sourcelines_DF[0]))
<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.
This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Comments are closed.