[ZODB-Dev] Re: setdefault branch
Ruslan Spivak
rspivak at nuxeo.com
Mon Aug 29 17:12:57 EDT 2005
"Tim Peters" <tim at zope.com> 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
More information about the ZODB-Dev
mailing list