Shove knowing about an object but unable to retrieve it

徘徊边缘 提交于 2019-12-11 08:26:08

问题


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:

  1. If I replace self.othername = "%s" % name by self.othername = name then 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?
  2. Irrespective of my workaround, how's it possible that data.keys() has my 'somename' key but yet data['somename'] does not work?
  3. Seems like shove_repro2.py still works if MyClass is 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

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