Accessing key in factory of defaultdict
问题 I am trying to do something similar to this: from collections import defaultdict import hashlib def factory(): key = 'aaa' return { 'key-md5' : hashlib.md5('%s' % (key)).hexdigest() } a = defaultdict(factory) print a['aaa'] (actually, the reason why I need access to the key in the factory is not to compute an md5 , but for other reasons; this is just an example) As you can see, in the factory I have no access to the key: I am just forcing it, which makes no sense whatsoever. Is it possible to