inspect.getsource()在python中不起作用

时间:2013-10-28 12:38:30

标签: python

我对此代码有疑问,但不是没有原因......

import inspect
inspect.getsource(min)

,错误是:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    inspect.getsource(min)
  File "C:\Python33\lib\inspect.py", line 726, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Python33\lib\inspect.py", line 715, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Python33\lib\inspect.py", line 551, in findsource
    file = getfile(object)
  File "C:\Python33\lib\inspect.py", line 435, in getfile
    'function, traceback, frame, or code object'.format(object))
TypeError: <built-in function min> is not a module, class, method, function, traceback, frame,or code object

1 个答案:

答案 0 :(得分:3)

内置的min()是用C代码实现的,inspect.getsource()只能显示 Python 代码:

>>> min
<built-in function min>

built-in function类型始终在C中实现。

此功能的代码来自bltinmodule.c源文件; builtin_min() function委托给同一源文件中的min_max() utility function