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

Tim Peters tim.one@home.com
Mon, 29 Jan 2001 21:19:12 -0500


[MAL]
> 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.

Do they find "for k, v in dict.items()" confusing now?  Would be the same.

> ...
> What we really want is iterators for dictionaries, so why not
> implement these instead of tweaking for-loops.

Seems an unrelated topic:  would "iterators for dictionaries" solve the
supposed problem with iteration order?

> 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.

Please quantify, if possible.  My belief (based on past experiments) is that
in loops fancier than

    for i in range(n):
        pass

the loop overhead quickly falls into the noise even now.

> Using an iterator object instead of the integer + __getitem__
> call machinery would allow more flexibility for all kinds of
> sequences or containers. ...

This is yet another abrupt change of topic, yes <0.9 wink>?  I agree a new
iteration *protocol* could have major attractions.