Closed and34 closed 10 years ago
You are right, sqlitedict serializes/deserializes entire objects, not just references. For example, the following will also work differently from the built-in dict
:
x = range(10)
d['a'] = x
x.append(-1)
print d['a'] # no -1!
d['a'] = x
print d['a'] # now there's -1 at the end
Can you think of a way to make this behaviour more explicit? So that this doesn't surprise sqlitedict users?
I found how the stdlib of Python handles this: a doc warning about mutable objects: http://docs.python.org/2/library/shelve.html ("Because of Python semantics...")
Could you write some disclaimer like that for sqlitedict docs too please?
What about to add something like this to methods:
def appendToList(self, key, val): if key in self: oldVal = self[key] oldVal.extend(val) self[key] = oldVal else: self[key] = [val]
It is an option. Then we should also add addToSet()
and addToDict()
etc. I guess.
But the disclaimer is needed either way, because most users will not know about these "special" methods.
I would also add: def setdefault(self, key, val): raise NotImplementedError('Use addTo* methods instead.')
-1 on that. There may be other patterns where people use setdefault, apart from setdefault + append
. This would throw them all away with NotImplementedError and a confusing message.
A fat disclaimer ought to be enough -- let's not go the Java way of assuming that users are stupid :)
-1 on adding these methods. In my opinion, the user should now how to handle these cases.
Added doc warning in c3385caf.
setdefault doesn't set list object in storage. I had to implement it manually like: