[Python-ideas] keyfuncdict -- the good, the bad, and the ugly
Raymond Hettinger
raymond.hettinger at gmail.com
Wed Jun 2 20:25:13 CEST 2010
Was thinking of all the things that could be done with Antoine's generalization:
The Good
========
d = keyfuncdict(key=str.lower) # case insensitive dict
def track(obj):
logging.info(obj)
return obj
d = keyfuncdict(key=track) # monitored dict
d = keyfuncdict(key=id) # makes benjamin happy
The Bad
=======
d = keyfuncdict(key=tuple) # lets you use lists as keys
d = keyfuncdict(key=repr) # support many kinds of mutable or unhashable keys
d = keyfuncdict(key=pickle.loads) # use anything picklable as a key
d = keyfuncdict(key=getuser) # track one most recent entry per user
The Ugly
========
d = keyfuncdict(key=random.random) # just plain weird
d = keyfuncdict(key=itertools.count().next) # all entries are unique and unretrievable ;-)
def remove(obj):
d.pop(obj)
return obj
d = keyfuncdict(key=remove) # self deleting dict ;-)
Raymond
More information about the Python-ideas
mailing list