How to find the location of the source code of an inline Python method?

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

+5
source share
2 answers

You can usually find the source files for the main python modules in the python installation folder itself. For example, on linux I can find the source code for the os module, which is a pretty popular python module in this place:

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

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

+4
source

To get the location of a Python file from a terminal:

 $ which python 

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

For instance:

 # 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 ... 
+2
source

Source: https://habr.com/ru/post/1238738/


All Articles