简体   繁体   English

为什么有些内置的Python函数只有pass?

[英]Why do some built-in Python functions only have pass?

I wanted to see how a math.py function was implemented, but when I opened the file in PyCharm I found that all the functions are empty and there is a simple pass .本来想看看一个math.py function是怎么实现的,结果打开PyCharm里面的文件发现里面所有的函数都是空的,只有一个简单的pass For example:例如:

def ceil(x): # real signature unknown; restored from __doc__
    """
    ceil(x)

    Return the ceiling of x as a float.
    This is the smallest integral value >= x.
    """
    pass

I guess it is because the functions being used are actually from the C standard library.我猜是因为使用的函数实际上来自 C 标准库。 How does it work?它是如何工作的?

PyCharm is lying to you. PyCharm 在骗你。 The source code you're looking at is a fake that PyCharm has created.您正在查看的源代码是 PyCharm 创建的伪造品。 PyCharm knows what functions should be there, and it can guess at their signatures using the function docstrings, but it has no idea what the function bodies should look like. PyCharm 知道应该有哪些函数,它可以使用函数文档字符串猜测它们的签名,但它不知道函数体应该是什么样子。

If you want to see the real source code, you can look at it in the official Github repository in Modules/mathmodule.c .如果你想看到真正的源代码,你可以在官方 Github 存储库中的Modules/mathmodule.c A lot of the functions in there are macro-generated thin wrappers around C functions from math.h , but there's also a bunch of manually-written code to handle things like inconsistent or insufficient standard library implementations, functions with no math.h equivalent, and customization hooks like __ceil__ .其中的许多函数都是围绕math.h C 函数的宏生成的瘦包装器,但也有一堆手动编写的代码来处理诸如不一致或不足的标准库实现、没有math.h等效函数的函数,以及像__ceil__这样的自定义钩子。

There are no source code written in python for some python libraries.对于一些python库,python中没有写源代码。 These libraries was written on C or another languages.这些库是用 C 或其他语言编写的。 They are not presented by .py files and PyCharm or any another IDE cannot open their sources in readable view.它们不是由.py文件提供的,PyCharm 或任何其他 IDE 无法以可读视图打开它们的源代码。

You can check sources in ...\AppData\Local\Programs\Python\Python310\Lib\site-packages .您可以在...\AppData\Local\Programs\Python\Python310\Lib\site-packages中检查来源。 Most likely in your-package folder there will be .pui, .pud, .dll and other similiar files很可能在your-package文件夹中会有.pui、.pud、.dll和其他类似的文件

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

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