python buildin 中的一些类中为什么方法的内容都是pass?
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 是怎么实现的。