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: