如何使用“pickle”

发布于 2024-08-17 05:44:14 字数 459 浏览 7 评论 0 原文

我的代码(我无法使用“pickle”):

class A(object):
    def __getstate__(self):
        print 'www'
        return 'sss'
    def __setstate__(self,d):
        print 'aaaa'

import pickle
a = A()
s = pickle.dumps(a)
e = pickle.loads(s)
print s,e

打印:

www
aaaa
ccopy_reg
_reconstructor
p0
(c__main__
A
p1
c__builtin__
object
p2
Ntp3
Rp4
S'sss'
p5
b. <__main__.A object at 0x00B08CF0>

谁可以告诉我如何使用。

my code(i was unable to use 'pickle'):

class A(object):
    def __getstate__(self):
        print 'www'
        return 'sss'
    def __setstate__(self,d):
        print 'aaaa'

import pickle
a = A()
s = pickle.dumps(a)
e = pickle.loads(s)
print s,e

print :

www
aaaa
ccopy_reg
_reconstructor
p0
(c__main__
A
p1
c__builtin__
object
p2
Ntp3
Rp4
S'sss'
p5
b. <__main__.A object at 0x00B08CF0>

who can tell me how to use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香橙ぽ 2024-08-24 05:44:14

你想做什么?它对我有用:

class A(object):
    def __init__(self):
        self.val = 100

    def __str__(self):
        """What a looks like if your print it"""
        return 'A:'+str(self.val)

import pickle
a = A()
a_pickled = pickle.dumps(a)
a.val = 200
a2 = pickle.loads(a_pickled)
print 'the original a'
print a
print # newline
print 'a2 - a clone of a before we changed the value'
print a2
print 

print 'Why are you trying to use __setstate__, not __init__?'
print

所以这将打印:

the original a
A:200

a2 - a clone of a before we changed the value
A:100

如果您需要 setstate:

class B(object):
    def __init__(self):
        print 'Perhaps __init__ must not happen twice?'
        print
        self.val = 100

    def __str__(self):
        """What a looks like if your print it"""
        return 'B:'+str(self.val)

    def __getstate__(self):
        return self.val

    def __setstate__(self,val):
        self.val = val

b = B()
b_pickled = pickle.dumps(b)
b.val = 200
b2 = pickle.loads(b_pickled)
print 'the original b'
print b
print # newline
print 'b2 - b clone of b before we changed the value'
print b2

打印:

Why are you trying to use __setstate__, not __init__?

Perhaps __init__ must not happen twice?

the original b
B:200

b2 - b clone of b before we changed the value
B:100

What are you trying to do? It works for me:

class A(object):
    def __init__(self):
        self.val = 100

    def __str__(self):
        """What a looks like if your print it"""
        return 'A:'+str(self.val)

import pickle
a = A()
a_pickled = pickle.dumps(a)
a.val = 200
a2 = pickle.loads(a_pickled)
print 'the original a'
print a
print # newline
print 'a2 - a clone of a before we changed the value'
print a2
print 

print 'Why are you trying to use __setstate__, not __init__?'
print

So this will print:

the original a
A:200

a2 - a clone of a before we changed the value
A:100

If you need setstate:

class B(object):
    def __init__(self):
        print 'Perhaps __init__ must not happen twice?'
        print
        self.val = 100

    def __str__(self):
        """What a looks like if your print it"""
        return 'B:'+str(self.val)

    def __getstate__(self):
        return self.val

    def __setstate__(self,val):
        self.val = val

b = B()
b_pickled = pickle.dumps(b)
b.val = 200
b2 = pickle.loads(b_pickled)
print 'the original b'
print b
print # newline
print 'b2 - b clone of b before we changed the value'
print b2

which prints:

Why are you trying to use __setstate__, not __init__?

Perhaps __init__ must not happen twice?

the original b
B:200

b2 - b clone of b before we changed the value
B:100
白芷 2024-08-24 05:44:14

您可以pickle(意思是,此代码按其应有的方式工作)。你似乎得到了一个结果,但你没有想到。如果您期望相同的“输出”,请尝试:

import pickle
a = A()
s = pickle.dumps(a)
e = pickle.loads(s)
print s, pickle.dumps(e)

您的示例不是典型的“pickling”示例。通常,腌制的对象会永久保存在某个地方或通过网络发送。参见例如 pickletest.pyhttp://www.sthurlow.com/ python/lesson10/.

pickling 有一些高级用法,请参阅 David Mertz XML 对象序列化文章:http://www.ibm.com/developerworks/xml/library/x-matters11.html

You are able to pickle (meaning, this code works as it should). You just seem to get a result, you don't expect. If you expect the same 'output', try:

import pickle
a = A()
s = pickle.dumps(a)
e = pickle.loads(s)
print s, pickle.dumps(e)

Your example isn't, well, a typical 'pickling' example. Usually pickled objects are saved somewhere persistently or sent over the wire. See e.g. pickletest.py: http://www.sthurlow.com/python/lesson10/.

There are advanced uses of pickling, see for example David Mertz XML object serialisation article: http://www.ibm.com/developerworks/xml/library/x-matters11.html

总以为 2024-08-24 05:44:14

简而言之,在您的示例中,e 等于 a。

不必关心这些奇怪的字符串,您可以转储这些字符串以保存到任何地方,只要记住当您加载它们时,您又得到了“a”对象。

In a nutshell, in your example, e equals a.

Don't have to care about these strang strings, you can dumps these strings to save to anywhere, just remember when you loads them, you got 'a' object again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文