[ZODB-Dev] BTree.setdefault(), BTree.pop()
Tim Peters
tim at zope.com
Mon Oct 4 11:45:07 EDT 2004
[Dmitry Vasiliev]
> Use case for .setdefault():
>
> >>> from BTrees.OOBTree import OOBTree
> >>> from persistent.list import PersistentList
> >>> t = OOBTree()
> >>> t.setdefault(key, PersistentList()).append(value)
Right, I understand what setdefault() does <wink>. The question is whether
people would actually *use* it, were it a BTree/Bucket method.
Experience in the Python core is that there are many more lines implementing
setdefault() methods than there are uses of it (setdefault() is only used
about a dozen times total in the core, across all dictionary-like objects).
The disincentives in practice are illustrated by your example:
- The line is hard to understand, in part because "setdefault" is
a poor name (e.g., "getorset()".
- It incurs the expenses of creating a new PersistentList object
every time, even when the key is already in t.
For those reasons, even in Python what you did above is still *usually*
spelled:
pl = t.get(key)
if pl is None:
pl = t[key] = PersistentList()
pl.append(value)
So I'm -0 on the idea, because I doubt it will be used. I understand that
you think you'll use it. Anyone else?
More information about the ZODB-Dev
mailing list