by cefstat · 12y ago · 0 comments
For example, if you provide a function that returns an empty list, and do:
l = foo['bar']
l.append('baz')
print len(foo['bar'])
That should print 2 (apologies if this isn't correct Python)If one had a defaultdict that took a default value in a language with enough reflection, you might be able to deduce that the lambda always returns a simple value such as 3.1415927, and not store copies in the dictionary.
>>> import collections
>>> dd = collections.defaultdict(int)
>>> print dd['doesnt exist']
0
>>> print dd
defaultdict(<type 'int'>, {'doesnt exist': 0})