Builtin dict should be callable, since a dict defines a funct ion

sismex01 at hebmex.com sismex01 at hebmex.com
Fri Dec 20 13:59:48 EST 2002


> From: bokr at oz.net [mailto:bokr at oz.net]
> Sent: Thursday, December 19, 2002 9:00 PM
> 
> On Thu, 19 Dec 2002 18:17:03 -0800, Erik Max Francis 
> <max at alcyone.com> wrote:
> 
> >Bengt Richter wrote:
> >
> > But why would this be helpful?  A dictionary implements a mappable
> > interface; a function implements are callable interface.  They're
> > different interfaces; why would dovetailing them into the 
> > same interface be beneficial?
>
> You can pass a reference to a dict to something that expects 
> a function, e.g., a sort. You could pass it as a memoized-result
> proxy function in some context and catch KeyError to take care of
> LRU caching update, etc.
>
> I'm sure many uses would turn up once the obstacle was removed, and
> people thought of functions as mappings and vice versa ;-)
>

You can already do this.

Let's say you need a function which works like a (callable)
dictionary for retrieving values. Instead of creating a new
function or subclassing dict, pass it a reference to:

    d.__getitem__

and voila, there's your reference to a function-behaving-like-
a-dictionary.

-gustavo




More information about the Python-list mailing list