为什么 inspect.getsource 在尝试获取 Python Built-in 的源代码时抛出 TypeError?

Why does inspect.getsource throw TypeError when trying to get source for Python Built-in?

python 内置对象和普通对象有什么区别?我们常说在 python 中一切皆对象。 例如,当我在 Python 3.6:

中执行此操作时
>>> import os, inspect
>>> inspect.getsource(os.scandir)
TypeError: <built-in function scandir> is not a module, class, method, function, traceback, frame, or code object

我有两个问题:

  1. 内置函数是对象吗?如果不是,这就是 getsource 抛出 TypeError 的原因吗?
  2. 为什么我在 python3 documentation 中找不到内置的 scandir?

您无法访问使用 C API 编写的内置函数和其他模块的源代码,因为它们没有 Python 源代码。

From the documentation for os.getsourcefile

Return the name of the Python source file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.