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?
Python code is not compiled into machine-code. It is compiled into a special low-level intermediary language called bytecode that only CPython understands. This code is stored in . pyc files in a hidden directory and cached for execution.
Python stores object in heap memory and reference of object in stack. Variables, functions stored in stack and object is stored in heap.
Double-clicking a . py script will execute the script (and may not send any useful results to the screen). To view the source you can open a . py file with IDLE (which comes with Python) or even Notepad although a more advanced text editor or IDE is recommended.
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
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With