更新local() [英] updating local()

查看:53
本文介绍了更新local()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我一次又一次地听到你没有_supposed_来更新

本地词典。


任何人都可以告诉我为什么,如果以下代码有效,我不应该这样做吗?




#扩展本地命名空间




def fun(a = 1,b = 2,** args):


print''locals:'',locals()

locals()。update(args)

print locals()


e = {''s'':3,''e'':4}

fun(k = 10,v = 32,** e)

谢谢,

$ b $bFlávio

Hi,

I heard time and again that you are not _supposed_ to update the
locals dictionary.

Can anyone tell me why, if the following code works, I should not do
this?

#
# Extending Local namespace
#

def fun(a=1,b=2,**args):

print ''locals:'',locals()
locals().update(args)
print locals()

e = {''s'':3,''e'':4}
fun(k=10,v=32,**e)
thanks,

Flávio

推荐答案

Flavio写道:


我一次又一次地听到你没有_supposed_更新
本地词典。

任何人都可以告诉我为什么,如果以下代码工作,我不应该这样做?


#扩展本地命名空间


def fun(a = 1, b = 2,** args):

pr int''locals:'',locals()
locals()。update(args)
print locals()

e = {''s'':3, ''e'':4}
有趣(k = 10,v = 32,** e)
Hi,

I heard time and again that you are not _supposed_ to update the
locals dictionary.

Can anyone tell me why, if the following code works, I should not do
this?

#
# Extending Local namespace
#

def fun(a=1,b=2,**args):

print ''locals:'',locals()
locals().update(args)
print locals()

e = {''s'':3,''e'':4}
fun(k=10,v=32,**e)



因为它取决于当前的实现而且不是保证

将来工作。


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/


Because it depends on the current implementation and isn''t guaranteeed
to work in the future.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


Flavio启发我们:
Flavio enlightened us with:
任何人都可以告诉我为什么,如果以下代码有效,我不应该这样做?

def fun(a = 1,b = 2,** args):
print''locals:'',locals()
locals()。update(args)
print loca ls()
Can anyone tell me why, if the following code works, I should not do
this?

def fun(a=1,b=2,**args):

print ''locals:'',locals()
locals().update(args)
print locals()




因为它非常非常非常不安全。如果某人

找到了调用该功能的方法,会发生什么?它可以替换

本地词典中的任何名称,包括__builtins__中的函数。在其他

字样中:可能整个程序可以通过其他代码接管

只需拨打一次该函数。


Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa



Because it''s very, very, very insecure. What would happen if someone
found a way to call that function? It could replace any name in the
locals dictionary, including functions from __builtins__. In other
words: probably the whole program could be taken over by other code by
just one call to that function.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


Flavio写道:
任何人都可以告诉我为什么,如果以下代码有效,我不应该这个?


#扩展本地命名空间


def fun(a = 1,b = 2,** args):

打印''当地人:'',当地人()
本地人()。更新(args)
打印当地人()

e = {''s'':3,'e'':4}
有趣(k = 10,v = 32,** e)
Can anyone tell me why, if the following code works, I should not do
this?

#
# Extending Local namespace
#

def fun(a=1,b=2,**args):

print ''locals:'',locals()
locals().update(args)
print locals()

e = {''s'':3,''e'':4}
fun(k=10,v=32,**e)



因为如果你想要的只是更新字典,你也可以使用

另一个字典,这不是本地人()。如果你想要的是更新

局部变量,那就不行了:


Because if all you want is to update a dictionary you might as well use
another dictionary which isn''t locals(). If what you want is to update the
local variables, that won''t work:

def fun(a = 1,b = 2,** args):
s = 1

print''locals:'',locals()

locals()。update(args)

print locals()

print s,e


e = {' 's'':3,'e'':4}
有趣(k = 10,v = 32,** e)
当地人:{'''':1,'' s'':1,''args'':{'s'':3,''e'':4,''k'':10,''v'':32},''b' ':

2}

{''a'':1,''b'':2,''e'':4,''k'' :10,''args'':{'s'':3,'''':4,''k'':10,''v'':

32} ''s'':1,''v'':32}

1 {'s'':3,''e'':4}


请注意,k,v和e被添加到本地词典中,但是真实的本地

变量如's''没有更新。此外,如果您尝试获取全局

变量(如果存在),则无法在函数中访问k,v和e />
作为局部变量。


此外,此行为未定义。不同版本的Python将以不同的方式运行
,即使是较小的代码更改也可以改变行为。使用

这个版本,'s'仍然没有更新,但我们现在可以访问''e''值

作为变量:

def fun(a = 1,b = 2,** args):
s = 1

print''locals:'',locals()

locals()。update(args)

print locals()

exec" print''hi''"

print s,e


fun(k = 10,v = 32,** e)
locals:{'''':1,'s '':1,''args'':{'s'':3,''e'':4,''k'':10,''v'':32},''b'' :

2}

{''a'':1,''b'':2,''e'':4,''k'': 10,''args'':{'s'':3,'''':4,''k'':10,''v'':

32}, ''s'':1,''v'':32}

hi

1 4
def fun(a=1,b=2,**args): s = 1
print ''locals:'',locals()
locals().update(args)
print locals()
print s, e

e = {''s'':3,''e'':4}
fun(k=10,v=32,**e) locals: {''a'': 1, ''s'': 1, ''args'': {''s'': 3, ''e'': 4, ''k'': 10, ''v'': 32}, ''b'':
2}
{''a'': 1, ''b'': 2, ''e'': 4, ''k'': 10, ''args'': {''s'': 3, ''e'': 4, ''k'': 10, ''v'':
32}, ''s'': 1, ''v'': 32}
1 {''s'': 3, ''e'': 4}
Note that k, v, and e are added to the locals dictionary, but real local
variables such as ''s'' are not updated. Also you cannot access k, v, and e
as local variables within the function, if you try you get the global
variables (if they exist).

Also, this behaviour is undefined. Different versions of Python will behave
differently, and even minor code changes can change the behaviour. With
this version, ''s'' still doesn''t update, but we can now access the ''e'' value
as a variable:
def fun(a=1,b=2,**args): s = 1
print ''locals:'',locals()
locals().update(args)
print locals()
exec "print ''hi''"
print s,e

fun(k=10,v=32,**e) locals: {''a'': 1, ''s'': 1, ''args'': {''s'': 3, ''e'': 4, ''k'': 10, ''v'': 32}, ''b'':
2}
{''a'': 1, ''b'': 2, ''e'': 4, ''k'': 10, ''args'': {''s'': 3, ''e'': 4, ''k'': 10, ''v'':
32}, ''s'': 1, ''v'': 32}
hi
1 4



简答:甚至不想尝试更新当地人()。



Short answer: don''t even think of trying to update locals().


这篇关于更新local()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆