[英]pass dict by reference to bit encoded list of functions
I seem to have an issue with a Manager.dict()
that gets passed to a list of functions (within a sub process) as when I modify it within the function, the new value isn't available outside. 我似乎将Manager.dict()
传递给函数列表(在子进程中)时遇到问题,因为当我在函数中修改它时,新值在外部不可用。 I create my list of functions like this: 我创建如下函数列表:
gwfuncs = [reboot, flush_macs, flush_cache, new_gw, revert_gw, send_log]
gw_func_dict = dict((chr(2**i), gwfuncs[i]) for i in xrange(0,min(len(gwfuncs),8)))
and then call it like this: 然后这样称呼它:
for bit in gw_func_dict.keys():
if gwupdate & ord(bit) == ord(bit):
gw_func_dict[bit](fh, maclist)
Now assume we're talking about flush_macs()
, whatever I do in the function to maclist, doesn't seem to be affecting the maclist outside of my function - why is this? 现在假设我们正在谈论flush_macs()
,无论我在函数中如何对maclist进行操作,似乎都不会影响我函数之外的maclist-为什么会这样? How can I modify it the way that my changes are available outside? 如何修改我的更改可在外部使用的方式?
==
has higher precedence than &
, so your if
statement really acts like this: ==
优先级比&
优先级高,因此您的if
语句实际上是这样的:
if gwupdate & (ord(bit) == ord(bit)):
Add some parentheses and it'll work: 添加一些括号即可使用:
if (gwupdate & ord(bit)) == ord(bit):
Also, you can simplify your code a little: 另外,您可以稍微简化一下代码:
gw_func_dict = dict((chr(2**i), func) for i, func in enumerate(gwfuncs[:8]))
And if you're using Python 2.7+: 如果您使用的是Python 2.7+:
gw_func_dict = {chr(2**i): func for i, func in enumerate(gwfuncs[:8])}
Also, iterating over a dictionary iterates over its keys by default, so you can remove .keys()
from your for
loop: 另外,默认情况下,对字典进行迭代会对其键进行迭代,因此您可以从for
循环中删除.keys()
:
for bit in gw_func_dict:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.