[ZODB-Dev] RE: setdefault branch

Tim Peters tim at zope.com
Mon Aug 29 15:02:47 EDT 2005


I'm trying to straighten out the new BTree.setdefault() code, on
ZODB/branches/alienoid-btrees_setdefault.

Turns out this can't work like Python's dict.setdefault() in all cases.
This is why:

>>> d = {}
>>> d.setdefault(666)
>>> d
{666: None}

That is, the default for the optional ``default`` argument is None, so when
the key is missing and no explicit default is given (or an explicit default
of None is given), the corrected code tries to associate None with the key.

That's fine for OO and IO trees, but can't work for II or OI or IF trees:
None isn't a possible value in the latter kinds of trees.

Proposal:  make BTree/Bucket setdefault() a two-argument function (3 if you
count ``self`` too).  In practice, an explicit default is always passed, and
not even in theory could we come up with a sane default-default that works
across all tree types.



More information about the ZODB-Dev mailing list