寻找内置 Python 函数的源代码?
有没有办法查看内置函数如何在 python 中工作?我不仅仅指如何使用它们,还指它们是如何构建的,排序或枚举背后的代码是什么等等...?
Is there a way to see how built in functions work in python? I don't mean just how to use them, but also how were they built, what is the code behind sorted or enumerate etc...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
由于 Python 是开源的,您可以阅读源代码。
要找出特定模块或函数在哪个文件中实现,通常可以打印
__file__
属性。或者,您可以使用inspect
模块,请参阅 inspect 文档中的“noreferrer">检索源代码。对于内置类和方法来说,这并不是那么简单,因为
inspect.getfile
和inspect.getsource
将返回一个类型错误,表明该对象是内置的。但是,许多内置类型可以在对象 Python 源码主干的子目录
。例如,请参阅此处了解枚举类的实现或此处用于
列表 类型。
Since Python is open source you can read the source code.
To find out what file a particular module or function is implemented in you can usually print the
__file__
attribute. Alternatively, you may use theinspect
module, see the section Retrieving Source Code in the documentation ofinspect
.For built-in classes and methods this is not so straightforward since
inspect.getfile
andinspect.getsource
will return a type error stating that the object is built-in. However, many of the built-in types can be found in theObjects
sub-directory of the Python source trunk. For example, see here for the implementation of the enumerate class or here for the implementation of thelist
type.我必须深入挖掘才能找到以下
内置函数
的来源,因为搜索会产生数千个结果。 (祝你好运,搜索其中任何一个以找到其源代码)无论如何,所有这些函数都在
bltinmodule.c
中定义,函数以builtin_{functionname}
开头I had to dig a little to find the source of the following
Built-in Functions
as the search would yield thousands of results. (Good luck searching for any of those to find where it's source is)Anyway, all those functions are defined in
bltinmodule.c
Functions start withbuiltin_{functionname}
这是一个cookbook答案来补充@Chris的答案,CPython已移至GitHub,Mercurial存储库将不再更新:
git 克隆 https://github.com/python/cpython.git
代码将检出到名为
cpython
的子目录 ->cd cpython
print()
的定义...egrep --color=always -R 'print' | less -R
builtin_print()
享受吧。
Here is a cookbook answer to supplement @Chris' answer, CPython has moved to GitHub and the Mercurial repository will no longer be updated:
git clone https://github.com/python/cpython.git
Code will checkout to a subdirectory called
cpython
->cd cpython
print()
...egrep --color=always -R 'print' | less -R
Python/bltinmodule.c
->builtin_print()
Enjoy.
iPython shell 使这一切变得简单:
function?
将为您提供文档。function??
还显示了代码。但这仅适用于纯 python 函数。然后您就可以随时下载 (c)Python 的源代码。
如果您对核心功能的 Pythonic 实现感兴趣,请查看 PyPy 源代码。
The iPython shell makes this easy:
function?
will give you the documentation.function??
shows also the code. BUT this only works for pure python functions.Then you can always download the source code for the (c)Python.
If you're interested in pythonic implementations of core functionality have a look at PyPy source.
2 种方法,
help()
检查有关代码段的使用情况,inspect
检查这些模块的隐藏代码1) inform:
use < strong>inpsect 模块来探索您想要的代码...
注意:您只能浏览已导入的模块(又称)包的代码,
例如:
2) help():
您只需使用
help( )
命令获取有关内置函数及其代码的帮助。例如:
如果您想查看 str() 的代码,只需输入 -
help(str)
它将像这样返回,
2 methods,
help()
inspect
1) inspect:
use inpsect module to explore code you want...
NOTE: you can able to explore code only for modules (aka) packages you have imported
for eg:
2) help():
you can simply use
help()
command to get help about builtin functions as well its code.for eg:
if you want to see the code for str() , simply type -
help(str)
it will return like this,
让我们直接回答你的问题。
查找内置 Python 函数的源代码?
源代码位于
cpython/Python/bltinmodule.c
要在 GitHub 存储库中查找源代码,请转至 此处。您可以看到所有内置函数都以
builtin_
开头,例如sorted()
是在builtin_sorted
中实现的。为了让您高兴,我将发布 实现 < code>sorted():
您可能已经注意到,这不是 Python代码,但是C代码。
Let's go straight to your question.
Finding the source code for built-in Python functions?
The source code is located at
cpython/Python/bltinmodule.c
To find the source code in the GitHub repository go here. You can see that all in-built functions start with
builtin_<name_of_function>
, for instance,sorted()
is implemented inbuiltin_sorted
.For your pleasure I'll post the implementation of
sorted()
:As you may have noticed, that's not Python code, but C code.
Python开发人员指南是一个相当未知的资源。
在(某种程度上)最近的 GH 问题 中,添加了一个新章节来解决该问题你问的是:CPython 源代码布局。如果发生变化,该资源也会更新。
Quite an unknown resource is the Python Developer Guide.
In a (somewhat) recent GH issue, a new chapter was added for to address the question you're asking: CPython Source Code Layout. If something should change, that resource will also get updated.
正如 @Jim 所提到的,文件组织在此处进行了描述。为了便于发现而转载:
As mentioned by @Jim, the file organization is described here. Reproduced for ease of discovery:
以下是引用自《CPython Internals Book》(撰写本书时的 Python 3.9)的目录布局:
* 内置函数位于
bltinmodule.c
文件内在/Python
目录中。Here is the directory layout quoted from the "CPython Internals Book"(Python 3.9 at the time of writing the book):
* the builtin functions are inside the
bltinmodule.c
file inside the/Python
directory.