Example 8.12. locals is read−only, globals is not
-
def foo(arg):
x = 1
print locals() -------------------------------------------------------------------1
locals()["x"] = 2-------------------------------------------------------------------2
print "x=",x -------------------------------------------------------------------3
z = 7
print "z=",z
foo(3)
globals()["z"] = 8 -------------------------------------------------------------------4
print "z=",z -------------------------------------------------------------------5
打印结果:
>>> ================================ RESTART ================================
>>>
z= 7
{'x': 1, 'arg': 3}
x= 1
z = 8
-
1
[原文]P111
Since foo is called with 3, this will print {'arg': 3, 'x': 1}. This should not be a surprise
[pope译]
由于foo调用时使用3,所以这里打印 {'arg': 3, 'x': 1}。这个没什么要吃惊的
[net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/locals_and_globals.html]
因为使用 3 来调用 foo,会打印出 {'arg': 3, 'x': 1}。这个应该没什么奇怪的。
locals()["x"] = 2
locals is a function that returns a dictionary, and here you are setting a value in that dictionary. You might think that this would change the value of the local variable x to 2, but it doesn't. locals does not actually return the local namespace, it returns a copy. So changing it does nothing to the value of the variables in the local namespace.
[popexizhi] 这种使用locals()使用的是一个copy ,只可以read-only
[pope译]
locals 是一个返回字典的函数,这里你给字典中设置了一个值。你可能认为这样可以改变local的参数x为2,但是不是这样的。locals并不是真的返回local命名空间,它返回一个拷贝。所以改变它,对local 命名空间没什么变量值的改变。
[net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/locals_and_globals.html]
locals 是一个返回 dictionary 的函数,这里您在 dictionary 中设置了一个值。您可能认为这样会改变局部变量 x 的值为 2,但并不会。locals 实际上没有返回局部名字空间,它返回的是一个拷贝。所以对它进行改变对局部名字空间中的变量值并无影响。
[popexizhi]
nothing to the value of the variables in the local namespace
[pope译]对local 命名空间没什么变量值的改变
[net 译] 对局部名字空间中的变量值并无影响
[popexizhi] nothing 在这里还是翻译为无影响比较顺畅一点儿:)
3[原文]P112
This prints x= 1, not x= 2
[pope译]
这里打印 x=1,而不是x=2
[net 译]
这样会打印出 x= 1,而不是 x= 2。
4[原文] P112
globals()["z"] = 8
After being burned by locals, you might think that this wouldn't change the value of z, but it does. Due to internal differences in how Python is implemented (which I'd rather not go into, since I don't fully understand them myself), globals returns the actual global namespace, not a copy: the exact opposite behavior of locals. So any changes to the dictionary returned by globals directly affect your global variables.
[?] globals()倒是可以修改,问题是why?看来作者也不是很明白(作者是谁啊?搜了一下也没发现,推荐去豆瓣看看)。why?如此设计,有什么用意呢?
[pope译]
在处理了locals后,你可能认为z也不会有什么变化,但它改变了。由于Python应用本身的不同(我最好还是不要深入谈这个问题,因为我自己也不是很了解其中的原因),globals 返回global 真实的命名空间,而不是一个拷贝,这个与locals的行为完成相反。所以对你的globals 返回字典的修改回直接影响你的全局变量的值
[net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/locals_and_globals.html ]
在有了对 locals 的经验之后,您可能认为这样不会 改变 z 的值,但是可以。由于 Python 在实现过程中内部有所区别 (关于这些区别我宁可不去研究,因为我自已还没有完全理解) ,globals 返回实际的全局名字空间,而不是一个拷贝:与 locals 的行为完全相反。所以对 globals 所返回的 dictionary 的任何的改动都会直接影响到全局变量。
5[原文]P112
This prints z= 8, not z= 7.
[pope译]
打印为z=8,而不是 z=7
[net 译]
这样会打印出 z= 8,而不是 z= 7。
-
1
没有评论:
发表评论