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

M.-A. Lemburg mal@lemburg.com
Mon, 29 Jan 2001 19:08:16 +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.
> > 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 ?
> Actually, I was going to propose to play dangerously here: the
>     for k:v in dict: ...
> syntax I proposed in my previous message should indeed expose
> PyDict_Next().  It should be a big speed-up, and I'm expecting (though
> don't have much proof) that most loops over dicts don't mutate the
> dict.
> Maybe we could add a flag to the dict that issues an error when a new
> key is inserted during such a for loop?  (I don't think the key order
> can be affected when a key is *deleted*.)

You mean: mark it read-only ? That would be a "nice to have"
property for a lot of mutable types indeed -- sort of like
low-level locks. This would be another candidate for an object flag
(much like the one Fred wants to introduce for weak referenced

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