Accessing Dictionaries VS Accessing Shelves

冷暖自知 提交于 2019-12-23 03:19:05

问题


Currently, I have a dictionary that has a number as the key and a Class as a value. I can access the attributes of that Class like so:

dictionary[str(instantiated_class_id_number)].attribute1

Due to memory issues, I want to use the shelve module. I am wondering if doing so is plausible. Does a shelve dictionary act the exact same as a standard dictionary? If not, how does it differ?


回答1:


Shelve doesn't act extactly the same as dictionary, notably when modifying objects that are already in the dictionary.

The difference is that when you add a class to a dictionary a reference is stored, but shelve keeps a pickled (serialized) copy of the object. If you then modify the object you will modify the in-memory copy, but not the pickled version. That can be handled (mostly) transparently by shelf.sync() and shelf.close(), which write out entries. Making all that work does require tracking all retrieved objects which haven't been written back yet so you do have to call shelf.sync() to clear the cache.

The problem with shelf.sync() clearing the cache is that you can keep a reference to the object and modify it again.

This code doesn't work as expected with a shelf, but will work with a dictionary:

s["foo"] = MyClass()
s["foo"].X = 8 
p = s["foo"] # store a reference to the object
p.X = 9 # update the reference
s.sync() # flushes the cache
p.X = 0
print "value in memory: %d" % p.X # prints 0
print "value in shelf: %d" % s["foo"].X # prints 9

Sync flushes the cache so the modified 'p' object is lost from the cache so it isn't written back.




回答2:


Yes, it is plausible:

Shelf objects support all methods supported by dictionaries. This eases the transition from dictionary based scripts to those requiring persistent storage.

You need to call shelf.sync() every so often to clear the cache.

EDIT

Take care, it's not exactly a dict. See e.g. Laurion's answer.

Oh, and you can only have str keys.



来源:https://stackoverflow.com/questions/3409856/accessing-dictionaries-vs-accessing-shelves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!