是否有纯粹用python编写的内置python功能?

发布于 2025-01-28 11:09:04 字数 122 浏览 4 评论 0原文

内置函数的功能定义,例如printinput等,因为它们已写在C中。在函数中,还是在Python中编写任何内置功能?

编辑:我专门谈论CPYTHON实现。

The function definitions for built-in functions such as print, input, etc., cannot be seen because they have been written in C. Is that the case for all built-in functions, or is there any built-in function that has been written in Python?

Edit: I am specifically talking about the CPython implementation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2025-02-04 11:09:04

几周前,我问自己同样的问题。这是我尝试的:

import inspect

for builtinname in dir(__builtins__):
    try:
        builtin = getattr(__builtins__, builtinname)
        src = inspect.getsource(builtin)
        srcfile = inspect.getsourcefile(builtin)
        print(f"Source for {builtinname} is in {srcfile} :")
        print(src)
    except TypeError as e:
        # inspect.getsource throw a TypeError when called on a builtin
        # it is not a documented behavior
        print(f"Source cannot be found for builtin {builtinname}")

唯一具有Python源可用的内置属性是__加载程序__。当您查看它时,您会知道它并不是真正的内置。

因此,您问题的最终答案是否定。在Cpython实施中,没有用纯Python编写的内置。

I asked myself the same question a few weeks ago. Here is what I tried :

import inspect

for builtinname in dir(__builtins__):
    try:
        builtin = getattr(__builtins__, builtinname)
        src = inspect.getsource(builtin)
        srcfile = inspect.getsourcefile(builtin)
        print(f"Source for {builtinname} is in {srcfile} :")
        print(src)
    except TypeError as e:
        # inspect.getsource throw a TypeError when called on a builtin
        # it is not a documented behavior
        print(f"Source cannot be found for builtin {builtinname}")

The only builtins attribute with python source availaible is __loader__. When you look about it you understand that it is not really a builtin.

So the final answer to your question is no. There is no builtin written in pure Python in the CPython implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文