python - 为什么当尝试获取 Python 内置源时,inspect.getsource 会抛出 TypeError ?

标签 python python-3.x python-internals

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 源代码。

来自 inspect.getsourcefile(object) 的文档:

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.

关于python - 为什么当尝试获取 Python 内置源时,inspect.getsource 会抛出 TypeError ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51166945/

相关文章:

Python smtplib sendmail() 不适用于主题/正文

python - 为什么 print() 在 Visual Studio Code 中不起作用?

python - 你如何让你的 Sprite 不在屏幕上复制自己?一旦文本出现,你如何让你的 Sprite 消失或重新开始?

Python 3.4 多处理队列比 Pipe 快,出乎意料

python - 为什么从列表创建列表会使列表变大?

python - 如何以编程方式检查 Windows 计算机上打开的浏览器实例?

python - 如何提高此图像中的 OCR 准确度?

python - 用上一个/下一个值 +- 100 填充 Na 的列

python - 从被测方法中导入的模块修补方法

python - 为什么 Python 在已经有 INPLACE_ADD 时发出 STORE_SUBSTR?