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

查看:106
本文介绍了为什么某些内置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.

如果您想查看真实的源代码,可以在

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天全站免登陆