为什么我的未勾选顺序和它被勾选时不同?

2024-04-25 02:13:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python变量persistence有点困惑,在我的代码中,我使用以下代码使模型参数在某些迭代过程中持久化

 with open('W_Hs_Hu_iter'+str(inx)+'.pickle', 'wb') as f:
        pickle.dump((self.W,self.Hs,self.Hu),f)

在长时间的迭代之后,我尝试用

with open('W_Hs_Hu_iter450.pickle', 'rb') as f:
    W,Hs,Hu= pickle.load(f)
    #W,Hu,Hs= pickle.load(f)

但我查过之后,Hs和Hu的顺序错了吗?那会发生吗?你知道吗


Tags: 代码模型self参数过程aswithload
2条回答

我同意@peterwood的说法,它看起来是正确的,而且我已经测试过了。你知道吗

import pickle

class TestObj(object):
    def __init__(self, one=1, two=2, three=3):
        self.one = one
        self.two = two
        self.three = three

    def save(self):
        with open('D:\\test.pickle', 'wb') as f:
            pickle.dump((self.one,self.two,self.three),f,-1)

    @staticmethod
    def load():
        with open('D:\\test.pickle', 'rb') as f:
            one,two,three = pickle.load(f)
        test_obj = TestObj(one, two, three)
        return test_obj

test_obj = TestObj() #Init with defaults
test_obj.save()
print 'after save: one: %s, two: %s, three: %s' % (test_obj.one,test_obj.two,test_obj.three)
test_obj = test_obj.load()
print 'after load: one: %s, two: %s, three: %s' % (test_obj.one,test_obj.two,test_obj.three)

我最好的猜测是,保存时,HsHu的值已经交换了。保存前进行日志\打印。你知道吗

下面是一个类实例的pickle,其中属性值在pickle之后交换。你知道吗

>>> class Foo(object):
...   a = 1
...   b = 2
...   def __init__(self, c,d):
...     self.c = c
...     self.d = d
...   def bar(self):
...     return self.a,self.b,self.c,self.d
... 
>>> f = Foo(3,4)
>>> _f = pickle.dumps(f)
>>> f.c,f.d = f.d,f.c
>>> f.b,f.a = f.a,f.b
>>> f_ = pickle.loads(_f)
>>> f_.bar()
(1, 2, 3, 4)
>>> f.bar()
(2, 1, 4, 3)

所以,这和预期的一样。但是,python通过引用来pickle类,因此如果更改类定义,它似乎会更改实例属性。你知道吗

>>> g = Foo(3,4)
>>> _g = pickle.dumps(g)
>>> g.c,g.d = g.d,g.c
>>> Foo.a,Foo.b = Foo.b,Foo.a
>>> g_ = pickle.loads(_g)
>>> g_.bar()
(2, 1, 3, 4)
>>> g.bar()
(2, 1, 4, 3)

如果将列表或其他序列作为类属性,则这一点更为明显。所以,让我们向类中添加一些列表对象。你知道吗

>>> Foo.a = []
>>> Foo.zap = lambda self:self.a
>>> Foo.baz = lambda self,x:self.a.append(x)
>>> 
>>> h = Foo(3,4)
>>> h.baz(0)
>>> h.baz(1)
>>> h.zap()
[0, 1]
>>> _h = pickle.dumps(h)
>>> h.baz(2)
>>> h.baz(3)
>>> h_ = pickle.loads(_h)
>>> h_.zap()
[0, 1, 2, 3]

这是因为列表的值实际上存储在class属性而不是instance属性上。这是这种行为的许多例子之一。你知道吗

如果您想保持实例“原样”(即不通过引用存储),那么应该使用dill进行pickle。然而,来自dill的泡菜会更大。你知道吗

>>> import dill
>>> _h = dill.dumps(h)
>>> h.baz(4)
>>> h.baz(5)
>>> h_ = dill.loads(_h)
>>> h_.zap()
[0, 1, 2, 3]
>>> h.zap()
[0, 1, 2, 3, 4, 5]

相关问题 更多 >