Pickle是否可以处理多个对象引用 [英] Can Pickle handle multiple object references

查看:63
本文介绍了Pickle是否可以处理多个对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有对象ab并且都引用了对象obj,那么当我腌制然后恢复对象时会发生什么?腌制的数据将知道" ab都引用了相同的对象并相应地恢复了所有内容,还是两者将得到两个不同的符号-?最初等于—对象?

If I have objects a and b and both reference object obj, what happens when I Pickle and then restore the objects? Will the pickled data 'know' that a and b both referenced the same object and restore everything accordingly, or will the two get two different — and initially equal — objects?

推荐答案

是的,共享库只会被序列化一次(pickle协议甚至可以处理循环引用).

Yes, shared objects will only get serialized once (the pickle protocol can even handle circular references).

文档:

pickle模块跟踪其已经存在的对象 序列化,以便以后对同一对象的引用将不再 再次序列化. marshal不这样做.

The pickle module keeps track of the objects it has already serialized, so that later references to the same object won’t be serialized again. marshal doesn’t do this.

这对于递归对象和对象共享都有影响. 递归对象是包含对其自身的引用的对象. 这些不是由marshal处理的,实际上是试图编组 递归对象将使您的Python解释器崩溃.对象共享 当在同一对象中有多个引用时发生 对象层次结构中要序列化的不同位置. pickle 仅将此类对象存储一次,并确保所有其他引用 指向主副本.共享对象保持共享状态,可以是 对于可变对象非常重要.

This has implications both for recursive objects and object sharing. Recursive objects are objects that contain references to themselves. These are not handled by marshal, and in fact, attempting to marshal recursive objects will crash your Python interpreter. Object sharing happens when there are multiple references to the same object in different places in the object hierarchy being serialized. pickle stores such objects only once, and ensures that all other references point to the master copy. Shared objects remain shared, which can be very important for mutable objects.

这篇关于Pickle是否可以处理多个对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆