为什么第一条语句返回NameError
,而max
可用
>>> __builtin__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__builtin__' is not defined
>>> max
<built-in function max>
>>> import __builtin__
>>> __builtin__.max
<built-in function max>
最佳答案
The builtins namespace associated with the execution of a code block is actually found by looking up the name
__builtins__
in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the__main__
module,__builtins__
is the built-in module__builtin__
(note: no ‘s’); when in any other module,__builtins__
is an alias for the dictionary of the__builtin__
module itself.__builtins__
can be set to a user-created dictionary to create a weak form of restricted execution.
所以它实际上是在查找 __builtins__
(因为你在主模块中)
>>> __builtins__.max
<built-in function max>
但如上所述,这只是 __builtin__
的别名(它不是主模块命名空间的一部分,尽管它已被 __builtins__
加载和引用).
关于python - __builtin__ 如何在运行时可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252457/