How to retrieve source code of Python functions
How to retrieve source code of Python functions
Learn to use the inspect and dill libraries to access Python functions' source code.
Subscribe now
Get the highlights in your inbox every week.
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.
- What is an IDE?
- Cheat sheet: Python 3.7 for beginners
- Top Python GUI frameworks
- Download: 7 essential PyPI libraries
- Red Hat Developers
- Latest Python content
- inspectis a built-in standard library
- dillis a third-party library
inspect
inspectis 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]] |
In [4]: | print[len[source_DF]] 218432 |
In [5]: | print[source_DF[:200]] class DataFrame[NDFrame]: |
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]]] 2 |
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]] |
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 errorIOError: could not get source codeand 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 notedillis not a standard library, so you must install it separately.
Its API is quite similar toinspect'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]]] 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 2 |
However, a big difference between dill and inspect is that dill's retrieving feature supports self-defined objects in the plain Python console.
Topics
About the author
Recommended reading
Anyone can compile open source code in these three simple steps
Tips for formatting when printing to console from C++
7 key components of observability in Python
How to package your Python code
10 eureka moments of coding in the community
What you need to know about containers for Python