Builtin dict should be callable, since a dict defines a function

Terry Reedy tjreedy at udel.edu
Thu Dec 19 21:46:00 EST 2002


"Bengt Richter" <bokr at oz.net> wrote in message
news:atttql$ouf$0 at 216.39.172.122...
> I posted the suggestion in a thread  Re: case-insensitive and
internationalized sort,
> but it occurs to me that the principle is arguable from the abstract
point of view.
> I.e., a dict implements a function key -> value, so why not let it
accept a normal
> function arg list (i.e., the tuple) as the argument for its key ->
value function?
> It should be a pretty simple change to provide __call__(self,
*args): return self[args]
> under the hood, and let hashability chips fall where they will via
[].

Do you mean something like this?

>>> d=callableDict({'o':1, 't':2, 'f':4})
>>> d
{'t': 2, 'o': 1, 'f': 4}
>>> d('o')
1

> The idea came up thinking about passing a dict in place of a
comparison function,
> where it might be practical to build a dict of all printable
character pair tuples
> and set the values according to how you want them ordered in a sort.

or this?

>>> class pairs(dict):
...   def  __call__(self,*key): return self[key]
...
>>> sorter = pairs({('a','b'):1})
>>> sorter('a','b')
1

You're right, it is simple.

Terry J. Reedy





More information about the Python-list mailing list