如何找到内置Python方法的源代码的位置?
有许多内置的Python方法,我想通过学习的源代码来理解。如何在我的计算机上找到它们的位置?是否有一些简单的命令可以在Python脚本或Linux上的终端中运行,用于查找内置方法的源文件?您通常可以在Python安装文件夹中找到核心Python模块的源文件。例如,在如何找到内置Python方法的源代码的位置?,python,Python,有许多内置的Python方法,我想通过学习的源代码来理解。如何在我的计算机上找到它们的位置?是否有一些简单的命令可以在Python脚本或Linux上的终端中运行,用于查找内置方法的源文件?您通常可以在Python安装文件夹中找到核心Python模块的源文件。例如,在linux上,我可以找到os模块的源代码,该模块在以下位置非常流行: /usr/lib/python2.7/os.py 如果您在windows,这通常是C:\python27\lib,但是您可以通过运行哪个python(linux)
linux
上,我可以找到os
模块的源代码,该模块在以下位置非常流行:
/usr/lib/python2.7/os.py
如果您在
windows
,这通常是C:\python27\lib
,但是您可以通过运行哪个python
(linux)和where python
(windows)来验证它,以从终端获取python的文件位置:
$ which python
但是,只需在函数后面附加??(注意,有些函数是C编译的,不是用Python编写的),就可以看到函数的源代码
例如:
# 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
...
您可能对我的示例感兴趣,它是内置的字符串方法isspace()。这不需要导入任何额外的核心模块。我在哪里可以找到它的源代码?出于性能方面的明显原因,内置函数和其他低级函数在C
中实现,因此它们只能以位编译的形式提供。但是,您仍然可以通过访问来查看这些函数的源代码。另一个答案是特别的,在/Objects/stringobject.c
,你可以找到你正在寻找的函数,string\u isspace()
。open???
对我不起作用,我得到了语法错误:无效的语法。Python 3.7.3.@x-yuri他可能正在使用IPython