PEP 234: Iterators
Just van Rossum
just at letterror.com
Tue May 1 14:27:26 EDT 2001
"Rainer Deyke" <root at rainerdeyke.com> writes:
[ getting an exception when you modify a dict during iteration ]
> It would be nice if this could be made reliable.
Michael Hudson wrote:
> Tricky, that. It could be made somewhat more reliable, but I think
> making it totally certain would have prohibitive drawbacks,
> particularly as stuff like:
>
> for i in d:
> d[i] += 1
>
> *is* supported.
I think you get a RuntimeError if the dictionary gets resized: the dict
iterator object can check cheaply for that event. Perhaps if the dict
object grew a magic counter, which would get incremented when a new
key is added or a key is deleted, and the dict iterator would check for
*that* value, this would be more reliable. I can't judge whether this
approach would be too expensive or not.
Just
More information about the Python-list
mailing list