python buildin 中的一些类中为什么方法的内容都是pass?

版权声明:码字不易,且看且珍惜!交流:Wechat: suihailiang0816 QQ: 931762054 转载并修改内容请与我联系 https://blog.csdn.net/weixin_41010198/article/details/89338224

python buildin 中的一些类中为什么方法的内容都是pass?

文章目录:


python 的源代码是用C语言写的


一、看到的一些方法的定义都是pass

例如:查看eval()函数的具体定义,在Pycharm中Ctrl + 鼠标左键 点中这个函数就会跳转到这个函数的定义处,如下:

def eval(*args, **kwargs): # real signature unknown
    """
    Evaluate the given source in the context of globals and locals.
    
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.
    """
    pass

在这里插入图片描述
可以看到这python的一个内置函数,但是并没有函数体的具体定义内容

python是C语言实现的,尽管有很多标准库是由python代码实现,但是涉及到底层支撑架构的功能还是C代码。 一些IDE为了对这些进行友好代码提示,会弄和底层一样的访问接口,而其实现直接写 pass 略过。

据我目前了解的 Python确实有时候遇到底层相关调用的时候 会使用cython进行编码 然后才使用 往往是通过.pyx结尾的文件 如果按住ctrl进行查找函数的话是跳转不进去的,相关具体内容可以查找cython的使用方法

二、如何查看Python的源代码

例如你想查看字典中定义的方法的具体源代码:

PyCharm在骗你,您正在查看的源代码是PyCharm创建的伪造品。 PyCharm知道哪些函数或object 应该在那里,它可以使用函数docstrings来猜测它们的签名,但是它不知道函数体应该是什么样的。python (特值cpython)是用c 语言写的。
如果您想查看字典真实的源代码,可以在 https://github.com/python/cpython/blob/master/Objects/dictobject.c 中看到dict 是怎么实现的。

猜你喜欢

转载自blog.csdn.net/weixin_41010198/article/details/89338224
今日推荐