python 3中内置函数的两个相互矛盾的含义(python 3.1,python 3k,python3000) [英] two conflicting meanings of builtins in python 3 (python 3.1, python 3k, python3000)
问题描述
我刚刚在comp.lang.python的查询下面发布了内容,但是我觉得这种问题在Stack Overflow上也有某种使用权,因此可以重复。本质:为什么 buildins在Python 3中有两种不同的解释?
I just posted below query to comp.lang.python, but i feel this kind of question has some kind of right-of-way here on Stack Overflow, too, so be it repeated. the essence: why does ‘builtins’ have two distinct interpretations in Python 3?
我很高兴接受有关
这句话的评论, http://celabs.com/python-3.1/reference/executionmodel.html,
的意思是,或者众神决定为什么要走这条路。我
期望这个名叫Kay Schluehr的人对此有发言权,或者
甚至BDFL都可能会想念
__ builtins __
正确解决他的失败,关注者和同伴的方法:
I would be very gladly accept any commentaries about what this
sentence, gleaned from http://celabs.com/python-3.1/reference/executionmodel.html,
is meant to mean, or why gods have decided this is the way to go. i
anticipate this guy named Kay Schluehr will have a say on that, or
maybe even the BDFL will care to pronounce __builtins__
the
correct way to his fallovers, followers, and fellownerds::
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.
过去至少有两个不同的术语,
内置(单数)和 buildins(复数形式),
中的一些以模块形式和字典形式同时存在(只是猜测)。现在
仅有 buildins
,因此幸运的是,
单数和复数之间的矛盾消失了-很好的摆脱了。
it used to be the case that there were at least two distinct terms,
‘builtin’ (in the singular) and ‘builtins’ (in the plural), some of
which existed both in module and in dict form (?just guessing?). now
there is only builtins
, so fortunately the ambivalence between
singular and plural has gone—good riddance.
,但为什么 __ builtins __
会根据
是否属于脚本的范围(即名称为$的模块)改变其含义b $ b存在,在调用 python foobar.py
时),或者这是辅助模块的
范围(直接导入还是执行,或者是
)是通过 foobar.py
间接访问的?我不明白其背后的原因
并感到非常困惑。
but why does __builtins__
change its meaning depending on whether
this is the scope of the ‘script’ (i.e. the module whose name was
present, when calling python foobar.py
) or whether this is the
scope of a secondary module (imported or executed, directly or
indirectly, by foobar.py
)? i cannot understand the reasoning
behind this and find it highly confusing.
理性:我为什么在乎?—我希望能够将姓名导出到我通过 exec(compile(get
其中,
(locator),locator)执行的python模块中未标记为
的全局命名空间(通过下划线
前缀) ,'exec'),R) R
应该去
来保存所述模块的私有名称。 是有点不可思议,但是
的基本练习是绕过python的导入系统并获得similr
结果...这都是关于将名称注入全全局以及
模块全局命名空间。
rationale: why do i care?—i want to be able to ‘export names to the
global namespace that were not marked private (by an underscore
prefix) in a python module that i execute via exec( compile( get
( locator ), locator, 'exec' ), R )
where R
is supposed to going
to hold the private names of said module’. it is a little arcane but
the basic exercise is to by-pass python’s import system and get similr
results... it is all about injecting names into the all-global and the
module-global namespaces.
推荐答案
getattr(__ builtins__,'__dict__', __builtins __)
应该给您要更新为将名称导出到全局名称空间的字典,无论 __ builtins __
是否是字典(然后没有 __ dict __
属性,因此 getattr
返回第三个参数,即字典 __builtins __
本身)或模块(然后确实具有该属性,而 getattr
返回它)。这是解决方法。至于为什么Python的文档工作方式需要这样一种复杂的解决方法,我将其归类为不幸的情况,因为实现问题暴露于用户可见(并确实是文档)级别(叹气)。可惜我们没有想到要在向Python 3的迁移中进行修复,但是现在进行向后不兼容的更改为时已晚:-(。
getattr(__builtins__, '__dict__', __builtins__)
should give you the dict that you want to update to "export names to the global namespace", whether __builtins__
is a dict (then it doesn't have a __dict__
attribute so getattr
returns the third argument, which is the dict __builtins__
itself) or a module (then it does have that attribute and getattr
returns it). This is the workaround. As to why Python's documented to work in a way requiring such a tangled workaround, I'd classify it as an unfortunate case of an implementation issue surfacing to user-visible (and indeed documented) level (sigh). Pity we didn't think of fixing it in the move to Python 3, but it's too late to make backwards-incompatible changes now:-(.
这篇关于python 3中内置函数的两个相互矛盾的含义(python 3.1,python 3k,python3000)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!