8

There are many built-in Python methods that I would like to study the source code of to understand. How can I find their location on my computer? Is there some simple command that I could run either in a Python script or in my terminal on my Linux that I could use to locate a built-in method's source file?

Rohan
  • 482
  • 6
  • 16

2 Answers2

5

You can usually find the source files for core python modules in the python installation folder itself. For instance, on linux, I can find the source code for os module which is a quite popular python module in this location:

/usr/lib/python2.7/os.py

If you are on windows, this is generally C:\python27\lib, but you can verify it for yourself by running which python in case of linux and where python in case of windows.

Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55
  • My example is the built-in string method isspace(). That does not require the importing of any additional core module. Where can I find the source for that? – Rohan Dec 20 '15 at 21:38
  • 1
    The built-ins and other low-level functions are implemented in `C` for the obvious reasons of performance, so they are only available in bit compiled form. However, you can still see the source code for these functions by visiting the [python source repo](https://hg.python.org/cpython/file/c6880edaf6f3). This other answer is [for Reference.](http://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions) – Prahlad Yeri Dec 21 '15 at 01:36
  • Particularly, in `/Objects/stringobject.c`, [here](https://hg.python.org/cpython/file/c6880edaf6f3/Objects/stringobject.c), you can find the function, `string_isspace()` - the one you are looking for. – Prahlad Yeri Dec 21 '15 at 01:43
3

To get the file location of Python from the terminal:

$ which python

But you can see the source code of a function simply by appending it with ?? (note that some functions are C compiled and not written in Python).

For example:

# Example 1:  Built in compiled function.
>>> open??
Docstring:
open(name[, mode[, buffering]]) -> file object

Open a file using the file() type, returns a file object.  This is the
preferred way to open a file.  See file.__doc__ for further information.
Type:      builtin_function_or_method

# Example 2:  Pandas function written in Python.
import pandas as pd
>>> pd.DataFrame??

Init signature: pd.DataFrame(self, data=None, index=None, columns=None, dtype=None, copy=False)
Source:
class DataFrame(NDFrame):

    """ Two-dimensional size-mutable, potentially heterogeneous tabular data
    structure with labeled axes (rows and columns). Arithmetic operations
    align on both row and column labels. Can be thought of as a dict-like
    container for Series objects. The primary pandas data structure

    Parameters
    ----------
    data : numpy ndarray (structured or homogeneous), dict, or DataFrame
        Dict can contain Series, arrays, constants, or list-like objects
    index : Index or array-like
        Index to use for resulting frame. Will default to np.arange(n) if
        no indexing information part of input data and no index provided
    columns : Index or array-like
        Column labels to use for resulting frame. Will default to
        np.arange(n) if no column labels are provided
    dtype : dtype, default None
        Data type to force, otherwise infer
    copy : boolean, default False
        Copy data from inputs. Only affects DataFrame / 2d ndarray input

    Examples
    --------
    >>> d = {'col1': ts1, 'col2': ts2}
    >>> df = DataFrame(data=d, index=index)
    >>> df2 = DataFrame(np.random.randn(10, 5))
    >>> df3 = DataFrame(np.random.randn(10, 5),
    ...                 columns=['a', 'b', 'c', 'd', 'e'])

    See also
    --------
    DataFrame.from_records : constructor from tuples, also record arrays
    DataFrame.from_dict : from dicts of Series, arrays, or dicts
    DataFrame.from_items : from sequence of (key, value) pairs
    pandas.read_csv, pandas.read_table, pandas.read_clipboard
    """

    @property
    def _constructor(self):
        return DataFrame

    _constructor_sliced = Series

    @property
    def _constructor_expanddim(self):
        from pandas.core.panel import Panel
        return Panel

    ...
Alexander
  • 105,104
  • 32
  • 201
  • 196