为什么有些内置的 Python 函数只有通过? [英] Why do some built-in Python functions only have pass?

查看:26
本文介绍了为什么有些内置的 Python 函数只有通过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看一个math.py函数是怎么实现的,但是当我在PyCharm中打开文件时发现所有函数都是空的,还有一个简单的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. 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

我猜这是因为所使用的函数实际上来自 C 标准库.它是如何工作的?

I guess it is because the functions being used are actually from the C standard library. How does it work?

推荐答案

PyCharm 在骗你.您正在查看的源代码是 PyCharm 创建的假代码.PyCharm 知道应该有哪些函数,它可以使用函数文档字符串猜测它们的签名,但它不知道函数体应该是什么样子.

PyCharm is lying to you. The source code you're looking at is a fake that PyCharm has created. 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.

如果你想看真正的源代码,你可以在Github官方仓库中查看Modules/mathmodule.c.里面的很多函数都是宏生成的对来自 math.h 的 C 函数的瘦包装,但也有一堆手动编写的代码来处理不一致或不充分的标准库实现、函数等问题没有 math.h 等效项,以及像 __ceil__ 这样的自定义钩子.

If you want to see the real source code, you can look at it in the official Github repository in 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__.

这篇关于为什么有些内置的 Python 函数只有通过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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