- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么我不能查看__builtins__
的源代码模块?
>>> import inspect
>>> inspect.getsource(__builtins__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/inspect.py", line 701, in getsource
lines, lnum = getsourcelines(object)
File "/usr/lib/python2.7/inspect.py", line 690, in getsourcelines
lines, lnum = findsource(object)
File "/usr/lib/python2.7/inspect.py", line 526, in findsource
file = getfile(object)
File "/usr/lib/python2.7/inspect.py", line 403, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__builtin__' (built-in)> is a built-in module
我经历了this谈话,但没有帮助。
如果我解释得好,这不应该发生:
>>> help(inspect.getsource)
Help on function getsource in module inspect:
getsource(object)
Return the text of the source code for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a single string. An
IOError is raised if the source code cannot be retrieved.
最佳答案
您只能通过这种方式检查 Python 源代码。使用 C API 编写的内置模块和扩展模块没有任何源代码,因此无法检查它。 (当你编译 C 代码时,结果可能有一些调试信息,包括用于构建它的文件的本地路径名,但它不包括实际的源文本。)
请注意,在您在文档中链接的函数的正上方,getsourcefile
说:
This will fail with a
TypeError
if the object is a built-in module, class, or function.
而且,正如您可能猜到的(或者可以通过查看从文档链接的 inspect.py
来验证),getsource
使用 getsourcefile
引擎盖下。
如果你在你的机器上本地构建 Python,并在构建后将源代码留在那里,有一个项目可以找到用于构建每个模块的 C 源代码,但我找不到它(我认为它在一个现在已经死了很久的 Berlios 或 Sourceforge),而且我认为它在 2.4 天左右之后没有更新过。
编写您自己的模块以在 github 存储库中查找源代码可能不会太难——或者,也许更好,在您自己的 github 存储库的本地克隆中。 (这比依赖本地构建的 Python 要好得多……)您甚至可以扩展它以使用 setuptools
信息来查找 pip
安装的扩展模块的源代码遵循某些常见的模式。但是,据我所知,还没有人发布过这样的模块。
如果您想自己构建类似的东西,请参阅 this quick&dirty proof of concept .尽管您可能想使用 git
或 Github API 而不是抓取,并且您希望能够搜索本地存储库(如果找不到,可能会克隆它)和/或缓存运行之间的事情,等等,这表明它是多么容易以及它需要多少特殊外壳。
因此,最好的选择是克隆 repo 并手动查找内容,或者直接在 github 上浏览。
关于python - 为什么不能查看 `__builtins__`模块的源码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50055640/
为什么我不能查看__builtins__的源代码模块? >>> import inspect >>> inspect.getsource(__builtins__) Traceback (most r
为什么我不能查看__builtins__的源代码模块? >>> import inspect >>> inspect.getsource(__builtins__) Traceback (most r
这里是我如何尝试覆盖 __builtins__ 函数: >>> lisa = __builtins__.list >>> list('123') ['1', '2', '3'] >>> 它按我预期的那
如果我编码有误并且我做了这样的事情: __builtins__ = 'abcd' 在我没有编写代码之前 import builtins 有没有办法将 __builtins__ 恢复到它的默认值? 最佳
如果你运行这段代码: src = "import os" d = dict(__builtins__={}) exec src in d Python 说: ImportError: __import
我把这个想法放在了How to make a cross-module variable?在 python3 中。并且懒得使用变量 __builtins__ 而不是模块 builtins。这应该没有什
我有以下脚本: a.py print(__builtins__.max) import b 和以下模块: b.py print(__builtins__.max) 使用 python3 a.py 启动
我正在使用内置模块插入一些实例,因此可以全局访问它们以进行调试。 __builtins__ 模块的问题是它是主脚本中的一个模块并且是模块中的一个字典,但是由于我的脚本取决于情况可以是主脚本或模块,我必
我今天正在编码并注意到一些东西。如果我打开一个新的解释器 session (IDLE)并检查 dir 函数定义的内容,我会得到: $ python >>> dir() ['__builtins__',
我可以让 python 打印 __builtins__ 的源代码吗?直接地? 或(更优选): __builtins__ 的源代码路径名是什么? ? 我至少知道以下几点: __builtins__是一个
我有一个包含以下内容的 python 脚本: # foo.py __builtins__ = 3 del __builtins__ print(int) # python3 -i foo.py
我正在探索 REPL,我注意到了 __builtins__ 模块。 我进去了 >>> __builtins__. 然后点击 Tab,然后 Python REPL 向我展示了一个内置标识符列表,包括ab
>>> eval('potato', {'__builtins__': None}) NameError: name 'potato' is not defined Python 2。有道理。 >>>
如果我打开交互模式并输入: __builtins__ = 0 # breaks everything 我是否完全中断了 session ?如果是这样,幕后发生了什么将 __builtins__ 分配给
我试图了解 eval 和 exec 如何处理给定的环境(全局变量和局部变量),所以我创建了一个类“logdict”,它的行为类似于字典,但记录了大多数方法(排除了 __new__): from fun
一个同学问了一个关于覆盖内置类字典的问题,经过一番摸索,我变得更加不确定。 拿内置的字典。我可以分配这个变量: >>> dict=5 >>> dict 5 现在我失去了对 dict 的访问权限(这会像
我想检查 __builtins__ 中列出的那些是否从 callable 返回 True(作为测试它们是否为函数的方法或不)。我希望返回 those 的列表Python 文档中的函数。我意识到其他项目
我现在正在尝试编码挑战。我之前无法更改代码del __builtins__.__dict__["__import__"] 但之后必须使用 import。我需要一种方法来恢复默认的 __builtins
我希望这不是一个太愚蠢的问题 :-( 但为什么 __import__ 没有包含在 inspect.getmembers(__builtins__) 中? 这是我尝试打印内置函数时得到的结果: >>>
我是一名优秀的程序员,十分优秀!