2015-05-03 89 views
0

最近我重構這個片段,我把它弄壞了:爲什麼`x + = a + d [x]`可以正常工作,但`x + = a; x + = d [x]`失敗?

  if from_mac in self.announces:          
       from_mac += '\\nAnnounces: ' + ',\\n'.join(self.announces[from_mac]) 
      if to_mac in self.announces:          
       to_mac += '\\nAnnounces: ' + ',\\n'.join(self.announces[to_mac]) 

我在重構之後,它看起來像這樣使線較短:

  if from_mac in self.announces:          
       from_mac += '\\nAnnounces: ' 
       from_mac += ',\\n'.join(self.announces[from_mac]) 
      if to_mac in self.announces:          
       to_mac += '\\nAnnounces: ' 
       to_mac += ',\\n'.join(self.announces[to_mac]) 

的類型變量是:

to_mac, from_mac -> string 
self.announces = defaultdict(list) # of strings 

此重構的不良影響是我得到像這樣的字符串:

"\nAnnounces: \nAnnounces: \nAnnounces: " 

可能是什麼原因呢?

語境

這裏的整體功能和它的輸出:

def print_report(self, skip_broadcast=False):         
    """                   
    Prints out a DOT file based on the gathered information.     
    """                   
    sys.stderr.write("%s\n" % repr(self.announces))        
    print("strict digraph {")             
    for from_mac in self.seen:             
     sys.stderr.write("k1=%s\n" % from_mac)         
     for to_mac in self.seen[from_mac]:          
      sys.stderr.write("k2=%s\n" % to_mac)        
      if skip_broadcast and (from_mac == '?' or to_mac == '?'):   
       continue              
      if from_mac in self.announces:          
       from_mac += '\\nAnnounces: '         
       from_mac += ',\\n'.join(self.announces[from_mac])    
      if to_mac in self.announces:          
       to_mac += '\\nAnnounces: '          
       to_mac += ',\\n'.join(self.announces[to_mac])     
      print('"%s" -> "%s";' % (from_mac, to_mac))       
    print("}") 

和輸出:

defaultdict(<type 'list'>, {'Cisco-Li_99:13:54\\n(58:6d:8f:99:13:54)': ['FajnaSiec']}) 
strict digraph { 
k1=Cisco-Li_99:13:54\n(58:6d:8f:99:13:54) 
k2=IPv6mcast_01\n(33:33:00:00:00:01) 
"Cisco-Li_99:13:54\n(58:6d:8f:99:13:54)\nAnnounces: " -> "IPv6mcast_01\n(33:33:00:00:00:01)"; 
k2=IPv4mcast_01\n(01:00:5e:00:00:01) 
"Cisco-Li_99:13:54\n(58:6d:8f:99:13:54)\nAnnounces: \nAnnounces: " -> "IPv4mcast_01\n(01:00:5e:00:00:01)"; 
k2=Tp-LinkT_20:74:8b\n(e8:94:f6:20:74:8b) 
"Cisco-Li_99:13:54\n(58:6d:8f:99:13:54)\nAnnounces: \nAnnounces: \nAnnounces: " -> "Tp-LinkT_20:74:8b\n(e8:94:f6:20:74:8b)"; 
k2=? 
"Cisco-Li_99:13:54\n(58:6d:8f:99:13:54)\nAnnounces: \nAnnounces: \nAnnounces: \nAnnounces: " -> "?"; 
k1=SamsungE_05:50:0e\n(00:e3:b2:05:50:0e) 
k2=Cisco-Li_99:13:54\n(58:6d:8f:99:13:54) 
"SamsungE_05:50:0e\n(00:e3:b2:05:50:0e)" -> "Cisco-Li_99:13:54\n(58:6d:8f:99:13:54)\nAnnounces: "; 
k1=? 
k2=Cisco-Li_99:13:54\n(58:6d:8f:99:13:54) 
"?" -> "Cisco-Li_99:13:54\n(58:6d:8f:99:13:54)\nAnnounces: "; 
k2=SamsungE_05:50:0e\n(00:e3:b2:05:50:0e) 
"?" -> "SamsungE_05:50:0e\n(00:e3:b2:05:50:0e)"; 
k2=? 
"?" -> "?"; 
} 
+2

在您的重構示例中,右手錶達式中'from_mac'的值與您原始代碼中的* *不同。換句話說,這不僅僅是'x + = a + b'的一個簡單情況,而且這裏'b'取決於'x'的值。所以'x + = a + f(x)'與* x + = a'相同,後面是'x + = f(x)',因爲第二種情況下'x'的值是*不同的*。 – lurker

+0

啊廢話!愚蠢的錯誤。謝謝! – d33tah

回答

1

由於用戶lurker在他的評論中說,這是因爲第一個表達式的變化from_mac是什麼,然後字典中的查找失敗。解決方案是引入一個新的變量,僅用於顯示。

相關問題