python defaultdict: 0 vs. int and [] vs list
问题 Is there any difference between passing int and lambda: 0 as arguments? Or between list and lambda: [] ? It looks like they do the same thing: from collections import defaultdict dint1 = defaultdict(lambda: 0) dint2 = defaultdict(int) dlist1 = defaultdict(lambda: []) dlist2 = defaultdict(list) for ch in 'abracadabra': dint1[ch] += 1 dint2[ch] += 1 dlist1[ch].append(1) dlist2[ch].append(1) print dint1.items() print dint2.items() print dlist1.items() print dlist2.items() ## -- Output: -- [('a',