[ZODB-Dev] lazy BTreeItems
Dieter Maurer
dieter at handshake.de
Fri Dec 9 14:32:35 EST 2005
Victor Safronovich wrote at 2005-12-9 12:05 +0500:
>Hello zodb-dev!
>
>Is this a correct behaviour?
>>>> from BTrees.IIBTree import IIBTree
>>>> t = IIBTree(map(None,range(5),range(5)))
>>>> list(t.keys())
>[0, 1, 2, 3, 4]
>>>> k = t.keys()
>>>> list(k)
>[0, 1, 2, 3, 4]
>>>> del t[0]
>>>> list(k)
>[]
>
>BUT
>>>> i = iter(t.keys())
>>>> del t[1]
>>>> list(i)
>[2, 3, 4]
Things like these are to be expected!
The BTrees methods "keys", "values" and "items" are implemented
as iterators.
You should *NEVER* change the data structure being iterated over.
If you do, funny things can happen -- not only with BTrees
but with *ANY* iterator.
E.g.
l = range(10)
for f in l: l.remove(f)
l
--> [1, 3, 5, 7, 9]
Due to the much more complex BTree structures, the results
of modifications during iteration might be even more surprising.
--
Dieter
More information about the ZODB-Dev
mailing list