简体   繁体   English

如何使用Python IDLE查看内置函数的代码?

[英]How do I view the code for built-in functions using the Python IDLE?

I'm trying to look at the source code of the pow() function, and would like to get to know my way around the Python34 directory. 我正在尝试查看pow()函数的源代码,并想了解我在Python34目录周围的方式。 I've looked at various libraries in the Lib directory such as fractions.py , and base64.py , but I can't seem to find the .py file where built-in functions are stored. 我已经看过Lib目录中的各种库,例如fractions.pybase64.py ,但是我似乎找不到用于存储内置函数的.py文件。 Where should I look? 我应该去哪里看?

Builtins and functions from the standard library are not always implemented in Python. 标准库中的内置函数和功能并非总是在Python中实现。 In CPython (the reference Python implementation), they're often written in C 在CPython(参考Python实现)中,它们通常是用C编写的

The method you are looking for is defined in the following files (depending on the type you're interested in): 您要寻找的方法在以下文件中定义(取决于您感兴趣的类型):

You can actually grep the Objects directory in the CPython source tree for /*nb_power*/ to find more. 实际上,您可以在CPython源代码树中为/*nb_power*/ grep Objects目录,以查找更多内容。 Try this search query on GitHub . 在GitHub上尝试此搜索查询

Functions which repr contain the wording "built-in" are not actually defined in Python code, in cPython - they are ratehr written in C, and amde available as binary modules. 其功能repr包含措辞“内置”实际上并没有在Python代码中定义的,在CPython的-他们ratehr用C语言编写,并能提供二进制模块通风口。

For example, when you get the repr of "pow" you get: 例如,当您获得“ pow”的代表时,您将获得:

>>> repr(pow)
'<built-in function pow>'

In contrast with a Python defined function: 与Python定义的函数相反:

>>> import glob
>>> repr (glob.glob)
'<function glob at 0x7efefe69aa70>'

So, you can search for the C source code of functions marked as "built-in" in the source code for Python itself, if you download it from Python.org (or fetch it with mercurial). 因此,如果您从Python.org下载(或以商业方式获取),则可以在Python本身的源代码中搜索标记为“内置”的C函数源代码。

If you want a pure Python implementation of any function in the satandard lib, that should be included with the pypy distribution - just fetch it from: 如果要在satandard lib中使用任何函数的纯Python实现,则应将其包含在pypy发行版中-只需从以下位置获取它:

https://bitbucket.org/pypy/pypy and you should see pure Python implementation of all functions that in cPython are implemented in C for performance or historic reasons. https://bitbucket.org/pypy/pypy ,您应该会看到出于性能或历史原因,在cPython中使用C实现的所有函数的纯Python实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM