如何在pycharm中查看python内置函数的实现? [英] How to view the implementation of python's built-in functions in pycharm?

查看:68
本文介绍了如何在pycharm中查看python内置函数的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在PyCharm中查看内置函数all()时,我只能在函数主体中看到"pass".如何查看实际的实现,以便我可以确切了解内置函数在做什么?

When I try to view the built-in function all() in PyCharm, I could just see "pass" in the function body. How to view the actual implementation so that I could know what exactly the built-in function is doing?

def all(*args, **kwargs): # real signature unknown
    """
    Return True if bool(x) is True for all values x in the iterable.

    If the iterable is empty, return True.
    """
    pass

推荐答案

假设您使用的是普通的CPython解释器, all 是一个内置函数对象,它仅具有一个指向已编译函数的指针静态链接到解释器(或libpython).对于大多数人来说,在该地址显示x86_64机器代码可能不会很有用.

Assuming you’re using the usual CPython interpreter, all is a builtin function object, which just has a pointer to a compiled function statically linked into the interpreter (or libpython). Showing you the x86_64 machine code at that address probably wouldn’t be very useful to the vast majority of people.

尝试在PyPy而不是CPython中运行代码.CPython中内置的许多东西都是PyPy中的普通旧Python代码. 1 当然,这并不总是一种选择(例如,PyPy还不支持3.7功能,方扩展模块仍然使用起来太慢,如果您使用的是不常见的平台,则很难构建自己的模型……),所以让我们回到CPython.

Try running your code in PyPy instead of CPython. Many things that are builtins in CPython are plain old Python code in PyPy.1 Of course that isn’t always an option (e.g., PyPy doesn’t support 3.7 features yet, there are a few third-party extension modules that are still way too slow to use, it’s harder to build yourself if you’re on some uncommon platform…), so let’s go back to CPython.

在网上很难找到该功能的实际C语言源.它位于 bltinmodule.c .但是,与标准库中Python模块的源代码不同,您可能没有这些文件.即使您拥有它们,二进制文件连接到源代码的唯一方法是调试从该源代码编译CPython时发出的输出,而您可能没有这样做.但是,如果您认为这听起来像是个好主意,那就是.自己构建CPython(您可能需要构建 Py_DEBUG ),然后可以在C源调试器/IDE中运行它,并且它可以处理所有笨拙的位.

The actual C source for that function isn’t too hard to find online. It’s in bltinmodule.c. But, unlike the source code to the Python modules in your standard library, you probably don’t have these files around. Even if you do have them, the only way to connect the binary to the source is through debugging output emitted when you compiled CPython from that source, which you probably didn’t do. But if you’re thinking that sounds like a great idea—it is. Build CPython yourself (you may want a Py_DEBUG build), and then you can just run it in your C source debugger/IDE and it can handle all the clumsy bits.

但是,即使您可以阅读基本的C代码并希望找到它,但听起来还是不那么乐于助人.

But if that sounds more scary than helpful, even though you can read basic C code and would like to find it…

我怎么知道在GitHub上哪里可以找到该代码?好吧,我知道回购在哪里;我知道将源代码组织成Python,对象,模块等的基本方式;我知道模块名称通常如何映射到C源文件名称.我知道buildins在某些方面很特别……

How did I know where to find that code on GitHub? Well, I know where the repo is; I know the basic organization of the source into Python, Objects, Modules, etc.; I know how module names usually map to C source file names; I know that builtins is special in a few ways…

这些都是非常简单的东西.您难道不可以将所有这些知识编程到一个脚本中,然后就可以用它构建一个PyCharm插件吗?

That’s all pretty simple stuff. Couldn’t you just program all that knowledge into a script, which you could then build a PyCharm plugin out of?

您可以通过快速的夜间hack进行前50%左右的操作,这些事情会在GitHub的海岸上乱扔.但是实际上,正确地做到这一点需要处理大量特殊情况,解析一些丑陋的C代码,等等.对于任何能够编写此类代码的人来说,仅使用lldb Python比编写它要容易得多.

You can do the first 50% or so in a quick evening hack, and such things litter the shores of GitHub. But actually doing it right requires handling a ton of special cases, parsing some ugly C code, etc. And for anyone capable of writing such a thing, it’s easier to just lldb Python than to write it.

1.此外,即使 内置的东西也是用Python和称为RPython的Python子集混合编写的,您可能会发现它们比C更容易理解-然后,通常很难找到该源,而看起来像Python的多个级别可能很难保持一致.

1. Also, even the things that are builtins are written in a mix of Python and a Python subset called RPython, which you might find easier to understand than C—then again, it’s often even harder to find that source, and the multiple levels that all look like Python can be hard to keep straight.

这篇关于如何在pycharm中查看python内置函数的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆