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 use defaultdict in a way that I can access the key in the factory?
defaultdict, is it only for correcting missing values? Because that's all it does aside from just returning{}? - Torxeddefaultdict, isn't it? The problem is that my (real) data structure, which I am storing in the defaultdict, has fields depending on the key. So, whenever I am trying to access a non-existing element, I need to create it in the factory using as a parameter the key of thedefaultdict. - blueFast