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

"Martin v. Löwis" martin at v.loewis.de
Fri Dec 20 07:16:24 EST 2002


Bengt Richter wrote:
> I recognize that distinct interfaces are often expressed with obj() vs obj[]
> and they very usefully lead to different aspects of the object. 

Not to mention "obj." Every object is a function, too, taking its 
attributes as arguments and returning a value (or AttributeError).

Your misconception is that you want to make one functional access to an 
object the preferred one. This is confusing:

In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.

IOW, why should d(key) be the same as d.__getitem__(key), instead of,
say, d.get(key); the latter has even the advantage of being a total 
function. This also points to a solution to your original question: If 
you have a dictionary and need a callable, use d.__getitem__.

Regards,
Martin






More information about the Python-list mailing list