问题
After a lot of playing around I found a workaround for some weird behaviour but would like to understand what's going on. Sorry if I am missing elementary stuff but I am pretty inexperienced in python.
So...
I am storing an object in Shove in one script and retrieving in another one - all works fine if I use my workaround
shove_repro_class.y
class MyClass():
def __init__(self, name ):
self.name = name
self.othername = "%s" % name ## <=== workaround for
## <=== self.othername = name
def __repr__(self):
return 'Object "%s"' % self.name
shove_repro.py (this is the first script to run)
from shove import Shove
from shove_repro_class import MyClass
location = 'file://test'
# location = 'sqlite:///test.db' # this won't work any better
data = Shove(location)
data['somename'] = MyClass('somename')
data.close()
shove_repro2.py (this is the second script to run)
from shove import Shove
from shove_repro_class import MyClass
location = 'file://test'
# location = 'sqlite:///test.db' ## same remark as before
data = Shove(location)
print data.keys() # prints [ 'somename' ]
print data[data.keys()[0]] # throws an exception in some cases (see 1. below) !
Couple of questions:
- If I replace
self.othername = "%s" % namebyself.othername = namethen I won't be able to retrieve the data from Shove. This workaround took me a long while to figure out and so I feel there's something fishy going out that I need to find out about otherwise I'm going to be surprised very often. I was thinking that maybe there's a deep/shallow copy issue here but given that I would think strings are immutable there should be no other possible copy than deep - right? - Irrespective of my workaround, how's it possible that
data.keys()has my'somename'key but yetdata['somename']does not work? - Seems like
shove_repro2.pystill works ifMyClassis not explicitly imported. Does this mean that the shove object stores the class definition?
Thx so much!
来源:https://stackoverflow.com/questions/11224538/shove-knowing-about-an-object-but-unable-to-retrieve-it