1

我对这段代码有疑问,但不知道为什么......

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
4

1 回答 1

5

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

>>> min
<built-in function min>

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

该函数的代码来自bltinmodule.c源文件;该builtin_min()函数委托给同一源文件中的min_max()实用程序函数

于 2013-10-28T12:39:41.013 回答