简体   繁体   中英

How can I find the location of the source code of a built-in Python method?

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?

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 .

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

    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM