0

我似乎有一个Manager.dict()传递给函数列表(在子进程内)的问题,因为当我在函数内修改它时,新值isn' t可在外面使用。 创建我的函数列表如下:通过参考位编码函数列表传递字典

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) 

现在假设我们正在谈论flush_macs(),无论我到maclist功能做的,没有按这似乎不会影响我的功能之外的男性主义者 - 这是为什么?我如何以外部可用的方式修改它?

回答

1

==具有precedence&高,所以你if声明真的就像这样:

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: