"Tim Peters" <[EMAIL PROTECTED]> writes: > 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} > Yes, currrently in branch in this case i simply return None without setting item, though of course it's not ideal.
> 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. > I think we can handle that without additional argument: if default is not set try to insert None, if we get TypeError exception then try to insert 0 as it's suitable both for int and floats(for floats it will be converted to 0.0 by default) Return type will be None, 0 or 0.0 or maybe just None? As for float type return i guess handling one more TypeError exception may be required to correctly differentiate between int and floats. Dunno if this is a good way, but at least it seems to be possible option. Ruslan _______________________________________________ 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