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
我有两个问题:
- 内置函数是对象吗?如果不是,这就是 getsource 抛出 TypeError 的原因吗?
- 为什么我在 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/