我刚把下面的问题发到编译lang.python,但我觉得这种问题在堆栈溢出问题上也有一定的方法,所以请重复。要点:为什么python3中的builtins有两种不同的解释?在
我很乐意接受任何关于这件事的评论
从http://celabs.com/python-3.1/reference/executionmodel.html收集的句子,
是为了卑鄙,或者为什么上帝决定这是一条路。我
估计这个叫凯·施鲁厄的人会有发言权,或者
也许即使是BDFL也会介意发__builtins__the
正确对待他的追随者、追随者和同僚们:The built-in 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 builtins;
when in any other module, __builtins__ is an alias for
the dictionary of the builtins module itself.
__builtins__ can be set to a user-created dictionary to
create a weak form of restricted execution.
过去至少有两个不同的术语,
“builtin”(单数)和“builtins”(复数),其中一些
它以模块和dict形式存在(?)?猜猜看。现在
只有builtins,所以幸运的是
单数和复数已经很好地摆脱了。在
但为什么__builtins__会根据
这是“脚本”(即名称为的模块)的范围
当调用python foobar.py)时出现,或者这是否是
辅助模块的范围(直接导入或执行,或
{cd5>间接?我不明白其中的道理
在这背后,我发现它非常令人困惑。在
理由:我为什么在乎?-我想把名字导出到
未标记为私有的全局命名空间(通过下划线
prefix)在我通过exec( compile( get
( locator ), locator, 'exec' ), R )执行的python模块中,R应该去的地方
持有所述模块的私人名称。它有点神秘但是
基本练习是绕过python的导入系统并获得similr
结果。。。这就是把名字注入到全球
模块全局命名空间。在