[Python-Dev] Re: Sets: elt in dict, lst.include
Mon, 29 Jan 2001 20:45:46 +0100
Guido van Rossum wrote:
> > > > 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...
> > >
> > > The same order that for k,v in dict.items() will yield, of course.
> > And then people find out that the order has some sorting
> > properties and start to use it... "how to sort a dictionary?"
> > comes up again, every now and then.
> I don't understand why you bring this up. We're not revealing
> anything new here, the random order of dict items has always been part
> of the language. The answer to "how to sort a dict" should be "copy
> it into a list and sort that."
> Or am I missing something?
I just wanted to hint at a problem which iterating over items
in an unordered set can cause. Especially new Python users will find
it confusing that the order of the items in an iteration can change
from one run to the next.
Not much of an argument, but I like explicit programming more
than magic under the cover. What we really want is iterators for
dictionaries, so why not implement these instead of tweaking
If you are looking for speedups w/r to for-loops, applying a
different indexing technique in for-loops would go a lot further
and provide better performance not only to dictionary loops,
but also to other sequences.
I have made some good experience with a special counter object
(sort of like a mutable integer) which is used instead of the
iteration index integer in the current implementation.
Using an iterator object instead of the integer + __getitem__
call machinery would allow more flexibility for all kinds of
sequences or containers. There could be an iterator type for
dictionaries, one for generic __getitem__ style sequences,
one for lists and tuples, etc. All of these could include
special logic to get the most out of the targetted datatype.
Well, just a thought...
Python Pages: http://www.lemburg.com/python/