use*_*865 114 python python-internals
有没有办法看到内置函数如何在python中工作?我不是指如何使用它们,而是它们是如何构建的,排序或枚举背后的代码是什么......?
Chr*_*ris 111
由于Python是开源的,您可以阅读源代码.
要查找特定模块或函数在哪个文件中实现,通常可以打印该__file__属性.或者,您可以使用该inspect模块,请参阅文档中的检索源代码部分inspect.
对于内置的类和方法,这不是因为这么简单的inspect.getfile和inspect.getsource会返回一个类型错误,指出对象是内置.但是,许多内置类型可以Objects在Python源中继的子目录中找到.例如,请参阅此处了解枚举类的实现,或者在此处查看list类型的实现.
kev*_*rpe 30
这是补充@Chris答案的食谱答案,CPython已移至GitHub,Mercurial存储库将不再更新:
git clone https://github.com/python/cpython.git
代码将签出到名为cpython- > 的子目录cd cpython
print()... 的定义egrep --color=always -R 'print' | less -RPython/bltinmodule.c- >builtin_print()请享用.
use*_*754 14
我不得不挖掘一点来找到以下的来源,Built-in Functions因为搜索会产生数千个结果.(祝你好运,寻找其中的任何一个,找到它的来源)
无论如何,所有这些函数都在bltinmodule.c函数的开头定义builtin_{functionname}
内置源:https://github.com/python/cpython/blob/master/Python/bltinmodule.c
对于内置类型:https: //github.com/python/cpython/tree/master/Objects
Guz*_*ero 11
让我们直接回答你的问题。
寻找内置 Python 函数的源代码?
源代码位于cpython/Python/bltinmodule.c
要在 GitHub 存储库中查找源代码,请转到此处。您可以看到所有以 开头的内置函数builtin_<name_of_function>,例如,sorted()都是在 中实现的builtin_sorted。
为了让您高兴,我将发布以下实现sorted():
builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *newlist, *v, *seq, *callable;
/* Keyword arguments are passed through list.sort() which will check
them. */
if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
return NULL;
newlist = PySequence_List(seq);
if (newlist == NULL)
return NULL;
callable = _PyObject_GetAttrId(newlist, &PyId_sort);
if (callable == NULL) {
Py_DECREF(newlist);
return NULL;
}
assert(nargs >= 1);
v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames);
Py_DECREF(callable);
if (v == NULL) {
Py_DECREF(newlist);
return NULL;
}
Py_DECREF(v);
return newlist;
}
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到,这不是 Python 代码,而是 C 代码。
正如 @Jim 所提到的,文件组织在此处进行了描述。为了便于发现而转载:
\n\n\n\n对于 Python 模块,典型的布局是:
\n\nRun Code Online (Sandbox Code Playgroud)\n\nLib/<module>.py\nModules/_<module>.c (if there\xe2\x80\x99s also a C accelerator module)\nLib/test/test_<module>.py\nDoc/library/<module>.rst\n对于仅扩展模块,典型布局是:
\n\nRun Code Online (Sandbox Code Playgroud)\n\nModules/<module>module.c\nLib/test/test_<module>.py\nDoc/library/<module>.rst\n对于内置类型,典型的布局是:
\n\nRun Code Online (Sandbox Code Playgroud)\n\nObjects/<builtin>object.c\nLib/test/test_<builtin>.py\nDoc/library/stdtypes.rst\n对于内置函数,典型的布局是:
\n\nRun Code Online (Sandbox Code Playgroud)\n\nPython/bltinmodule.c\nLib/test/test_builtin.py\nDoc/library/functions.rst\n一些例外:
\n\nRun Code Online (Sandbox Code Playgroud)\nbuiltin type int is at Objects/longobject.c\nbuiltin type str is at Objects/unicodeobject.c\nbuiltin module sys is at Python/sysmodule.c\nbuiltin module marshal is at Python/marshal.c\nWindows-only module winreg is at PC/winreg.c\n
2种方法
help()inspect 1)检查:
使用inpsect模块来浏览所需的代码... 注意:您只能浏览已导入的模块(aka)软件包的代码
例如:
>>> import randint
>>> from inspect import getsource
>>> getsource(randint) # here i am going to explore code for package called `randint`
Run Code Online (Sandbox Code Playgroud)
2)help():
您只需使用help()命令即可获得有关内置函数及其代码的帮助。
例如:如果您想查看str()的代码,只需键入- help(str)
它会像这样返回
>>> help(str)
Help on class str in module __builtin__:
class str(basestring)
| str(object='') -> string
|
| Return a nice string representation of the object.
| If the argument is a string, the return value is the same object.
|
| Method resolution order:
| str
| basestring
| object
|
| Methods defined here:
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __contains__(...)
| x.__contains__(y) <==> y in x
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __format__(...)
| S.__format__(format_spec) -> string
|
| Return a formatted version of S as described by format_spec.
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
|
| __getattribute__(...)
-- More --
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87307 次 |
| 最近记录: |