在使用python的过程中想看某些方法的源码实现可以用『ctrl+鼠标左键』的方法追踪到该方法的定义处。但是很多时候会发现某些方法下面只有pass占位符,并没有源码实现:
def abs(*args, **kwargs): # real signature unknown
""" Return the absolute value of the argument. """
pass
def all(*args, **kwargs): # real signature unknown
"""
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
"""
pass
这是为何呢?
查阅了一些资料后发现:python中很多源码是采用C语言实现的,也就是这种情况下用『ctrl+鼠标左键』追踪到的并不是真正的源码。而之所以这样设计,目的是为了用『ctrl+鼠标左键』的方法跳转追踪时或者用help()函数时查看描述信息,告诉使用者这个函数是干什么的:
如,help(abs)时,运行后结果返回:
abs(x, /)
Return the absolute value of the argument.
python如何可以用C实现可以进入python官方网站参考官方文档Extending and Embedding