社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python中filter函数的代码在哪里?[副本]

Maik • 5 年前 • 1927 次点击  

有没有办法看看内置函数在python中是如何工作的?我不是指如何使用它们,而是指它们是如何构建的,背后的代码是什么 已排序 列举 等。。。?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/47289
 
1927 次点击  
文章 [ 7 ]  |  最新文章 5 年前
Mateen Ulhaq
Reply   •   1 楼
Mateen Ulhaq    5 年前

正如@jim提到的,文件组织被描述为 here .为便于发现而复制:

对于python模块,典型的布局是:

Lib/<module>.py
Modules/_<module>.c (if there’s also a C accelerator module)
Lib/test/test_<module>.py
Doc/library/<module>.rst

对于仅扩展模块,典型布局为:

Modules/<module>module.c
Lib/test/test_<module>.py
Doc/library/<module>.rst

对于内置类型,典型布局为:

Objects/<builtin>object.c
Lib/test/test_<builtin>.py
Doc/library/stdtypes.rst

对于内置功能,典型布局为:

Python/bltinmodule.c
Lib/test/test_builtin.py
Doc/library/functions.rst

一些例外情况:

builtin type int is at Objects/longobject.c
builtin type str is at Objects/unicodeobject.c
builtin module sys is at Python/sysmodule.c
builtin module marshal is at Python/marshal.c
Windows-only module winreg is at PC/winreg.c
Jim Fasarakis Hilliard
Reply   •   2 楼
Jim Fasarakis Hilliard    7 年前

python是一个相当未知的资源 Developer Guide 是的。

最近的生长激素 issue ,添加了一个新的章节来解决您提出的问题: CPython Source Code Layout 是的。如果发生变化,该资源也将得到更新。

Mohideen bin Mohammed
Reply   •   3 楼
Mohideen bin Mohammed    6 年前

两种方法,

  1. 您可以使用 help()
  2. 您可以使用 inspect

1)检查:

使用 输入 模块来探索您想要的代码… 注: 您只能浏览已导入的模块(aka)包的代码

例如:

  >>> import randint  
  >>> from inspect import getsource
  >>> getsource(randint) # here i am going to explore code for package called `randint`

2)帮助():

你可以简单地使用 帮助() 命令获取有关内置函数及其代码的帮助。

例如: 如果要查看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  --
user1767754
Reply   •   4 楼
user1767754    6 年前

enter image description here

我得挖一点才能找到下面的来源 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

tback
Reply   •   5 楼
tback    13 年前

这个 iPython Shell让这一切变得简单: function? 会给你文件的。 function?? 同时显示代码。但这只适用于纯python函数。

那你就可以永远 download (c)python的源代码。

如果您对核心功能的pythonic实现感兴趣,请查看 PyPy 来源。

Stevoisiak kevinarpe
Reply   •   6 楼
Stevoisiak kevinarpe    7 年前

这是一本食谱的补充 @Chris' answer ,cpython已移动到github,Mercurial存储库将不再更新:

  1. 必要时安装git。
  2. git clone https://github.com/python/cpython.git

  3. 代码将签出到名为 cpython -> cd cpython

  4. 假设我们正在寻找 print()
  5. egrep --color=always -R 'print' | less -R
  6. 啊哈!见 Python/bltinmodule.c -> builtin_print()

享受吧。

Rob Rose Chris
Reply   •   7 楼
Rob Rose Chris    7 年前

因为python是开源的,所以您可以阅读 source code 是的。

要了解某个特定模块或函数在哪个文件中实现,通常可以打印 __file__ 属性。或者,您可以使用 inspect 模块,参见章节 Retrieving Source Code 在文件中 检查 是的。

对于内置类和方法来说,这并不是那么简单,因为 inspect.getfile inspect.getsource 将返回一个类型错误,指出该对象是内置的。但是,许多内置类型可以在 Objects sub-directory of the Python source trunk .例如,请参见 here 对于枚举类的实现,或者 here 为了实施 list 键入。