[Python-Dev] Re: Sets: elt in dict, lst.include

M.-A. Lemburg mal@lemburg.com
Mon, 29 Jan 2001 14:28:24 +0100


Tim Peters wrote:
> 
> [Ping]
> >     dict[key] = 1
> >     if key in dict: ...
> >     for key in dict: ...
> 
> [Guido]
> > No chance of a time-machine escape, but I *can* say that I agree that
> > Ping's proposal makes a lot of sense.  This is a reversal of my
> > previous opinion on this matter.  (Take note -- those don't happen
> > very often! :-)
> >
> > First to submit a working patch gets a free copy of 2.1a2 and
> > subsequent releases,
> 
> Thomas since submitted a patch to do the "if key in dict" part (which I
> reviewed and accepted, pending resolution of doc issues).
> 
> It does not do the "for key in dict" part.  It's not entirely clear whether
> you intended to approve that part too (I've simplified away many layers of
> quoting in the above <wink>).  In any case, nobody is working on that part.
> 
> WRT that part, Ping produced some stats in:
> 
> http://mail.python.org/pipermail/python-dev/2001-January/012106.html
> 
> > How often do you write 'dict.has_key(x)'?          (std lib says: 206)
> > How often do you write 'for x in dict.keys()'?     (std lib says: 49)
> >
> > How often do you write 'x in dict.values()'?       (std lib says: 0)
> > How often do you write 'for x in dict.values()'?   (std lib says: 3)
> 
> However, he did not report on occurrences of
> 
>     for k, v in dict.items()
> 
> I'm not clear exactly which files he examined in the above, or how the
> counts were obtained.  So I don't know how this compares:  I counted 188
> instances of the string ".items(" in 122 .py files, under the dist/ portion
> of current CVS.  A number of those were assignment and return stmts, others
> were dict.items() in an arglist, and at least one was in a comment.  After
> weeding those out, I was left with 153 legit "for" loops iterating over
> x.items().  In all:
> 
>     153 iterating over x.items()
>     118     "     over x.keys()
>      17     "     over x.values()
> 
> So I conclude that iterating over .values() is significantly more common
> than iterating over .keys().
> 
> On c.l.py about an hour ago, Thomas complained that two (out of two) of his
> coworkers guessed wrong about what
> 
>     for x in dict:
> 
> would do, but didn't say what they *did* think it would do.  Since Thomas
> doesn't work with idiots, I'm guessing they *didn't* guess it would iterate
> over either values or the lines of a freshly-opened file named "dict"
> <wink>.
> 
> So if you did intend to approve "for x in dict" iterating over dict.keys(),
> maybe you want to call me out on that "approval post" I forged under your
> name.

Dictionaries are not sequences. I wonder what order a user of
for k,v in dict: (or whatever other of this proposal you choose)
will expect...

Please also take into account that dictionaries are *mutable*
and their internal state is not defined to e.g. not change due to
lookups (take the string optimization for example...), so exposing
PyDict_Next() in any to Python will cause trouble. In the end,
you will need to create a list or tuple to iterate over one way
or another, so why bother overloading for-loops w/r to dictionaries ?

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company:                                        http://www.egenix.com/
Consulting:                                    http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/