why should dict not be callable?

Kent Johnson kent at kentsjohnson.com
Tue Oct 17 15:33:58 EDT 2006


georgeryoung at gmail.com wrote:
> A couple of times recently I've come across this problem:  I have a
> large list to sort and I need to the the "key=function" argument to
> sort appropriately.  But I actually have the key mapping in a big
> dictionary.  Now I have to make an intermediary function:
> 
> def key_fn(key):
>    return key_dict[key]

Try key=key_dict.__getitem__

In [3]: d=dict(a=1,b=2)

In [4]: d.__getitem__('a')
Out[4]: 1

Kent



More information about the Python-list mailing list