[prev in list] [next in list] [prev in thread] [next in thread]
List: zodb-dev
Subject: RE: [ZODB-Dev] Is this a bug?
From: "Tim Peters" <tim () zope ! com>
Date: 2004-03-21 6:43:57
Message-ID: LNBBLJKPBEHFEDALKOLCGENPJKAB.tim () zope ! com
[Download RAW message or body]
[Chandra Tambrin]
> I've just tried out ZoDB. I was trying to do the following:
>
> from BTrees.OOBTree import OOBTree
> t = OOBTree()
>
> for i in range(10):
> t[i] = str(i)
>
> for each in t.keys():
> del t[each]
>
> len(t)
> --> 5
>
> should be 0 right? what happened here?
You're mutating a BTree while iterating over it, and the end result isn't
well defined. It's a lot like doing
L = range(10)
for x in L:
L.remove(x)
in Python, mutating a Python list while iterating over it (try that! what
does L contain at the end?).
Don't be fooled into assuming that BTree.keys() is like a Python
dict.keys() -- it isn't. BTree.keys() is more like a Python
dict.iterkeys(): you get back a tiny iterator object, not a potentially
giant list of keys.
If you have to mutate a BTree while iterating over it, you'll need to
materialize the full collection of keys first:
for each in list(t.keys()):
del t[each]
or
for each in tuple(t.keys()):
del t[each]
both leave t empty. In either case, the thing you're iterating over is no
longer the BTree then, but a distinct data structure computed from the
original BTree.
_______________________________________________
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/
ZODB-Dev mailing list - ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic