durusmail: durus-users: del btree[key] ?
del btree[key] ?
2005-07-11
del btree[key] ?
Mario Ruggier
2005-07-11
> Mario Ruggier wrote:
>> Hi,
>>
>> deleting items in a btree behaves surprisingly, when the key is complex.
>> Here is a little sequence of commands that shows what I mean. Why isn't
>> that one remaining item not deleted?
>
> Because mutating a BTree while iterating its keys is a no-no.

> Note the difference in "for key in b" and "for key in b.keys()" is that
> the latter creates a new list of keys that is independent from the
> BTree, whereas the former makes use of iterkeys(), I believe.

Yep, that's it... since my recent question about how best to implement
btree.has_key(), I have now made it an automatism to do "for key in btree"
whenever i want to iterate on the keys ;-(

Doing what I tried to do with a dict, i.e. for key in dict: del
dirct[key], raises a RuntimeError, which is nice feedback ;) Maybe Durus
BTree should also raise this when a btree changes during iteration?

Thanks...

mario

reply