"in" for dicts (was: Python 2.1 function attributes)

Nathaniel Gray n8gray at nospam.nospam
Mon Jan 29 02:18:16 EST 2001


Tom Satter wrote:

> "Nathaniel Gray" <n8gray at caltech.edu> wrote in message
> news:3A7496A7.253B7B67 at caltech.edu...
> ...
> >
> > If I was to ask you, "Tim, is 'larch' in Webster's dictionary?" would
> > you reply, "Yes, it's under 'L'", or would you reply, "No, there's no
> > 'larch' section, just A,B,C,D,..."?
> >
> > (I know you'd _actually_ reply "Look it up yourself!" ;^)
> >
> > "in" just doesn't imply "has_key" for dictionaries.
> 
> Except that in Webster's dictionary, 'larch' IS a key with the definition
> (basically a tree) being the value.  So I would expect that 'larch' is IN
> Webster's.

Ahh.  Nothing like using a clever analogy to prove just the opposite of 
your point.  :^)  It just goes to show, once again, that one should never 
post to usenet when one's in a rush.

Nevertheless, there are still three big problems with "in" for dictionaries:

Number 1:  There is no reason, a priori, to think that "in dict" should 
mean "in dict.keys()" rather than "in dict.values()" or even "in 
dict.items()".  Explicit is better than implicit, right?

Number 2:  The chosen semantics for "in dict" are inconsistent with the 
meaning of "in" in the context of lists.  The "keys" of a list are the 
integers from 0 to len(the list) - 1.  When I write:
        list1 = []
        list2 = ['dead', 'parrot']
        for thing in list2:
                list1.append(thing)
        print list1
I get ['dead', 'parrot'], the list's "values", not [1, 2].  Naively, one 
would then expect that:
        list1 = []
        dict1 = {1:'dead', 2:'parrot'}
        for thing in dict1:
                list1.append(thing)
        print list1
should either print ['dead', 'parrot'] or ['parrot', 'dead'].  With lists, 
"in" refers to the thing being stored, not the thing you use to _get_ the 
thing being stored.  Why should dictionaries be different?  It's 
counterintuitive.

Number 5:  (three, sir)  Oh yes, number 3:  We've already got "for a_key in 
dict.keys()"!  The only reason we're introducing these ambiguous, 
inconsistent semantics is so that we can use a dictionary as a set and 
still say "for thing in a_set:".  (I sure hope we're not doing it just to 
save a few keystrokes every now and then!)  We're not straightening out an 
awkward idiom or clearing up a Python wart, we're masquerading one data 
structure as another.  "It's not a big dog.  Perhaps if we clipped it's 
ears and taught it to meow it could pass for a cat!"

Once again, wouldn't it just be better to have a collections module for 
Python with a Set class and let dictionaries be dictionaries?

And yes, I dislike list.pop() too.  ;^)

Lest I come off as too much of a grumpy Gus, I should add that I really 
like most of the new stuff in Python 2.1a1, particularly xreadlines.  I'll 
be first in line to upgrade when 2.1 stabilizes, even if "in dict" 
survives.

-n8

-- 
_.~'^`~._.~'^`~._.~'^`~._.~'^`~._.~'^`~._
             Nathaniel Gray
   California Institute of Technology
     Computation and Neural Systems
     n8gray <at> caltech <dot> edu
_.~'^`~._.~'^`~._.~'^`~._.~'^`~._.~'^`~._




More information about the Python-list mailing list