解压缩一个没有文件的泡菜变量?

时间:2018-10-16 17:47:59

标签: python pickle

我使用以下代码在不打开文件的情况下腌制了python变量,但没有文件我似乎无法解压缩它。

import pickle

class Foo:
   attr = 'a class attr'

picklestring = pickle.dumps(Foo)

print(picklestring)

1 个答案:

答案 0 :(得分:2)

要从字节对象释放,请使用pickle.loads

In [3]: import pickle
   ...:
   ...: class Foo:
   ...:    attr = 'a class attr'
   ...:
   ...: picklestring = pickle.dumps(Foo)
   ...:
   ...: print(picklestring)
   ...:
b'\x80\x03c__main__\nFoo\nq\x00.'

In [4]: pickle.loads(picklestring)
Out[4]: __main__.Foo