Python 通过引用将dict传递给位编码的函数列表

Python 通过引用将dict传递给位编码的函数列表,python,dictionary,multiprocessing,pass-by-reference,python-multiprocessing,Python,Dictionary,Multiprocessing,Pass By Reference,Python Multiprocessing,我似乎对传递到函数列表(在子进程中)的Manager.dict()有问题,因为当我在函数中修改它时,新值在外部不可用。 我创建的函数列表如下所示: 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))) for bit in gw_func

我似乎对传递到函数列表(在子进程中)的
Manager.dict()有问题,因为当我在函数中修改它时,新值在外部不可用。
我创建的函数列表如下所示:

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)))
for bit in gw_func_dict.keys():
    if gwupdate & ord(bit) == ord(bit):
        gw_func_dict[bit](fh, maclist)
if gwupdate & (ord(bit) == ord(bit)):
然后这样称呼它:

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)))
for bit in gw_func_dict.keys():
    if gwupdate & ord(bit) == ord(bit):
        gw_func_dict[bit](fh, maclist)
if gwupdate & (ord(bit) == ord(bit)):

现在假设我们讨论的是
flush_macs()
,无论我在函数to maclist中做什么,似乎都不会影响函数之外的maclist-为什么?我如何修改它,使我的更改在外部可用?

=
的值高于
&
,因此您的
if
语句实际上是这样的:

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)))
for bit in gw_func_dict.keys():
    if gwupdate & ord(bit) == ord(bit):
        gw_func_dict[bit](fh, maclist)
if gwupdate & (ord(bit) == ord(bit)):
添加一些括号,它就会工作:

if (gwupdate & ord(bit)) == ord(bit):
此外,您还可以稍微简化代码:

gw_func_dict = dict((chr(2**i), func) for i, func in enumerate(gwfuncs[:8]))
如果您使用的是Python 2.7+:

gw_func_dict = {chr(2**i): func for i, func in enumerate(gwfuncs[:8])}
此外,默认情况下,迭代字典会迭代字典的键,因此您可以从
for
循环中删除
.keys()

for bit in gw_func_dict: