全局命名空间中的funcs vs vars [英] funcs vs vars in global namespace

查看:70
本文介绍了全局命名空间中的funcs vs vars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天没有回答,我已经做了一些调查,而且我显然不明白python命名空间是如何工作的。这是一个测试

程序:


#!/ usr / bin / python


b = 2 < br $>

def sumWithGlobal(a):

返回+ b


#这很容易

打印''案例1:普通函数调用:''

打印sumWithGlobal(2)

打印


#如果你没有发送带有eval的全局字典,它会使用来自

#的调用程序,所以这与案例1基本相同

print' '案例2:使用相同的globals()从eval内部调用函数:''

print eval(" sumWithGlobal(2)")

print


#自从我发送一个全局字典但没有包含一个定义

#for sumWithGlobal(),显然这将失败(注释掉得到

#past错误)

print''案例3:尝试仅替换全局变量b(对于已知的

原因失败) )''

#print eval(" sumWithGlobal(2)&q uot;,{''b'':3})

打印


#Here定义sumWithGlobals但不是b,它仍然有效。为什么?

#难道我不能得到b未定义的错误,因为它不是在eval的

#globals字典中?

print''案例4:尝试设置函数sumWithGlobal(成功

,原因不明''

print eval(" sumWithGlobal(2) ",{''sumWithGlobal'':sumWithGlobal})

打印


#最后我定义了两个,但我仍然得到输出,好像b = 2

#Again,为什么?在eval'的全局中,b = 3,不是吗?

print''案例5:尝试同时设置这两个功能和var。(var有错误的值)''

print eval(" sumWithGlobal(2)",''sumWithGlobal'':sumWithGlobal,'b'':3})

打印

如果我向sumWithGlobal添加一个print globals(),我会在
$中看到{''b'':2} b $ b案例1,2,4和5.我做错了什么?

Getting no answer yesterday, I''ve done some investigation and I
obviously don''t understand how python namespaces work. Here''s a test
program:

#!/usr/bin/python

b = 2

def sumWithGlobal(a):
return a + b

#this is easy
print ''Case 1: Normal function call:''
print sumWithGlobal(2)
print

#If you don''t send a globals dict with eval, it uses the one from
#the calling program, so this is basically the same as Case 1
print ''Case 2: Function call from inside an eval using the same globals():''
print eval("sumWithGlobal(2)")
print

#Since I''m sending in a globals dict but haven''t included a defintion
#for sumWithGlobal(), obviously this will fail (commented out to get
#past the error)
print ''Case 3: Attempt to replace just the global var b (fails for known
reason)''
#print eval("sumWithGlobal(2)", {''b'':3})
print

#Here is define sumWithGlobals but not b and it still works. Why?
#Shouldn''t I get an error that b is undefined, since it isn''t in the
#globals dict of the eval?
print ''Case 4: Attempt to set just the function sumWithGlobal (succeeds
for unknown reason''
print eval("sumWithGlobal(2)", {''sumWithGlobal'':sumWithGlobal})
print

#And finally I define both but I still get output as if b = 2
#Again, why? In the eval''s global, b = 3, doesn''t it?
print ''Case 5: Attempt to set both function and var. (var has wrong value)''
print eval("sumWithGlobal(2)", {''sumWithGlobal'':sumWithGlobal, ''b'':3})
print
If I add a "print globals()" to sumWithGlobal, I see {''b'':2} in there in
cases 1, 2, 4 and 5. What am I doing wrong?

推荐答案

David Rysdam< dr ***** @ ll.mit .edu>写道:

...
David Rysdam <dr*****@ll.mit.edu> wrote:
...
def sumWithGlobal(a):
返回a + b
... #Here is define sumWithGlobals但不是b,它仍然有效。为什么?
#难道我不能得到b未定义的错误,因为它不在eval的
#globals字典中吗?
print''案例4:尝试设置函数sumWithGlobal(成功
因为未知原因''
打印eval(sumWithGlobal(2),{''sumWithGlobal'':sumWithGlobal})
打印


函数sumWithGlobal在内部使用

的全局字典定义它所在的模块,而不是它被称为模块的模块

来自,包括你在eval中的kinda sorta pseudo模块。

这也解释了:

如果我添加一个print globals ()对于sumWithGlobal,我在
案例1,2,4和5中看到{''b'':2}。我做错了什么?
def sumWithGlobal(a):
return a + b ... #Here is define sumWithGlobals but not b and it still works. Why?
#Shouldn''t I get an error that b is undefined, since it isn''t in the
#globals dict of the eval?
print ''Case 4: Attempt to set just the function sumWithGlobal (succeeds
for unknown reason''
print eval("sumWithGlobal(2)", {''sumWithGlobal'':sumWithGlobal})
print
The function sumWithGlobal, internally, uses the global dictionary of
the module it was defined in, NOT that of the module it''s being called
from, including the "kinda sorta pseudo" module you have within an eval.
This also explains:
If I add a "print globals()" to sumWithGlobal, I see {''b'':2} in there in
cases 1, 2, 4 and 5. What am I doing wrong?




你误解了Python全局:它们是每个模块,而不是跨越所有模块的b / b。

Alex



You''re misunderstanding Python globals: they are per-module, not "across
all modules".
Alex


Alex Martel li写道:
Alex Martelli wrote:
David Rysdam< dr ***** @ ll.mit.edu>写道:
...
David Rysdam <dr*****@ll.mit.edu> wrote:
...
def sumWithGlobal(a):
返回a + b
def sumWithGlobal(a):
return a + b



...



...

#这里定义sumWithGlobals但不是b,它仍然有效。为什么?
#难道我不能得到b未定义的错误,因为它不在eval的
#globals字典中吗?
print''案例4:尝试设置函数sumWithGlobal(成功
因为未知原因''
打印eval(sumWithGlobal(2),{''sumWithGlobal'':sumWithGlobal})
print
#Here is define sumWithGlobals but not b and it still works. Why?
#Shouldn''t I get an error that b is undefined, since it isn''t in the
#globals dict of the eval?
print ''Case 4: Attempt to set just the function sumWithGlobal (succeeds
for unknown reason''
print eval("sumWithGlobal(2)", {''sumWithGlobal'':sumWithGlobal})
print



函数sumWithGlobal,在内部,使用它所定义的模块的全局字典,而不是它被称为
的模块,包括你在eval中有一个kinda sorta pseudo模块。
这也解释了:


The function sumWithGlobal, internally, uses the global dictionary of
the module it was defined in, NOT that of the module it''s being called
from, including the "kinda sorta pseudo" module you have within an eval.
This also explains:

如果我添加一个print globals()为了总结全球,我在那里看到{''b'':2}
案例1,2,4和5.我做错了什么?
If I add a "print globals()" to sumWithGlobal, I see {''b'':2} in there in
cases 1, 2, 4 and 5. What am I doing wrong?



你' 误解了Python全局:它们是每个模块,而不是所有模块。

Alex


You''re misunderstanding Python globals: they are per-module, not "across
all modules".
Alex




OK ,我加n有点理解。我不知道是什么意思是什么时候能够为eval()指定全局变量,但是如果它没有做到

我想要它不能做我想要的。


我想做的是能够运行多个使用相同的脚本

全局变量和函数名称,但能够拥有我的主脚本

以编程方式定义这些变量和函数。我可以用rexec沙箱的东西吗?
?或者rexec更多关于保留

exec()代码而不是精确指定它可以做什么?



OK, I can kind of understand that. I''m not sure what the point of being
able to specify the globals for an eval() is then, but if it doesn''t do
what I want it doesn''t do what I want.

What I want to do is be able to run multiple scripts that use the same
global variable and function names but be able to have my master script
define those variables and functions programmatically. Can I do that
with the rexec sandbox stuff? Or is rexec more about keeping the
exec()''d code from doing things than specifying precisely what it can do?


David Rysdam <博士***** @ ll.mit.edu>写道:

...
David Rysdam <dr*****@ll.mit.edu> wrote:
...
你误解了Python全局:它们是每个模块,而不是跨越
所有模块。
好的,我可以理解这一点。我不确定能够为eval()指定全局变量的意义是什么,但是如果它没有做我想要的东西它不会做什么我想要。
You''re misunderstanding Python globals: they are per-module, not "across
all modules".
OK, I can kind of understand that. I''m not sure what the point of being
able to specify the globals for an eval() is then, but if it doesn''t do
what I want it doesn''t do what I want.




当你传递给eval的是一个表达时,这一点更清楚:


eval( ''a + b'',dict(a = 23,b = 99))。


我想要做的是能够运行多个使用相同的脚本全局变量和函数名称但能够使用我的主脚本
以编程方式定义这些变量和函数。我可以用rexec沙盒的东西吗?或者rexec更多关于保持
exec()'代码不做事情而不是精确指定它能做什么?



The point is clearer when what you''re passing to eval is an expression:

eval(''a+b'', dict(a=23, b=99)).

What I want to do is be able to run multiple scripts that use the same
global variable and function names but be able to have my master script
define those variables and functions programmatically. Can I do that
with the rexec sandbox stuff? Or is rexec more about keeping the
exec()''d code from doing things than specifying precisely what it can do?




rexec已被删除,因为它没有提供安全性

据称提供(这是为了防止代码做坏事,但是它确实没有这么做),叹了口气。 />

你可以做你想要的,例如通过在你正在调用的函数的模块对象中设置你想要的值 - 那个模块对象'
字典是函数的全局命名空间。说:


sub_module = __import __(which_one_this_time)

vars(sub_module).update(which_dict_this_time)

print sub_module.the_function( 23)


还有其他方法,但很少有像这个一样直接。

Alex



rexec has been removed because it did not offer the security it
purported to offer (it was about keeping code from doing bad things, but
it reallly didn''t), sigh.

You can do what you want for example by setting the values you want in
the module object of the function you''re calling -- that module object''s
dictionary is the function''s global namespace. Say:

sub_module = __import__(which_one_this_time)
vars(sub_module).update(which_dict_this_time)
print sub_module.the_function(23)

There are other ways, but few are as direct as this one.
Alex


这篇关于全局命名空间中的funcs vs vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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