用泡菜更新 [英] Update with pickle

查看:74
本文介绍了用泡菜更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用pickle模块更新对象而不是

创建新实例?

Thx

Nicolas

Is it possible to use the pickle module to update an object instead of
creating a new instance?
Thx
Nicolas

推荐答案

Nicolas Fleury写道:
Nicolas Fleury wrote:
是否可以使用pickle模块更新对象而不是创建
一个新的实例?
Is it possible to use the pickle module to update an object instead of
creating a new instance?




我不知道你想做什么。你能更好地解释一下吗?什么

你的意思是更新一个对象?


-

Ciao,

Matteo



I don''t get what you want to do. Can you explain yourself better? What
do you mean by "updating" an object?

--
Ciao,
Matteo


Matteo Dell''Amico写道:
Matteo Dell''Amico wrote:
Nicolas Fleury写道:
Nicolas Fleury wrote:
是否可以使用pickle模块更新对象而不是
创建一个新实例?
Is it possible to use the pickle module to update an object instead of
creating a new instance?



我不知道你想做什么。你能更好地解释一下吗?什么
是什么意思更新一个对象?


I don''t get what you want to do. Can you explain yourself better? What
do you mean by "updating" an object?




使用相同的引用而不是创建新实例。基本上

使pickle调用现有对象的__init__。我现在看到的最好的解决方案是将新对象的__dict__复制到现有的

之一。我想用同一类型的转储对象

来更新对象的内容,以便对现有对象的所有引用仍然有效。


问候,

Nicolas



Use the same reference instead of creating a new instance. Basically
making pickle call __init__ of an existing object. The best solution I
see for now is to copy the __dict__ of the new object to the existing
one. I want to update the content of an object with a dumped object of
the same type so that all reference to existing object are still valid.

Regards,
Nicolas


Nicolas Fleury写道:
Nicolas Fleury wrote:
我没有得到你想做什么。你能更好地解释一下吗?什么
是什么意思更新对象?
I don''t get what you want to do. Can you explain yourself better? What
do you mean by "updating" an object?


使用相同的引用而不是创建新实例。基本上
使pickle调用现有对象的__init__。我现在看到的最佳解决方案是将新对象的__dict__复制到现有的
。我想用相同类型的转储对象更新对象的内容,以便所有对现有对象的引用仍然有效。


Use the same reference instead of creating a new instance. Basically
making pickle call __init__ of an existing object. The best solution I
see for now is to copy the __dict__ of the new object to the existing
one. I want to update the content of an object with a dumped object of
the same type so that all reference to existing object are still valid.




我认为它不可能自动完成。谨防复制

__dict__,因为从python 2.2开始,可能有数据

并不存在于__dict__中,例如在使用__slots__时。


我想你必须找到一种方法来封装信息,也许一个

代理可能对你没用(我找不到食谱在Python中

Cookbook现在,因为它似乎已经关闭了)。这样,你可以

保留对代理的所有引用,并在你需要它时更改封装数据。


-

Ciao,

Matteo



I think it''s not possible to do automatically. Beware of copying
__dict__, since starting with python 2.2, it''s possible to have data
that doesn''t reside in __dict__, for instance when using __slots__.

I guess you have to find a way to encapsulate information, and maybe a
proxy could be OK for you (I can''t find the recipe in the Python
Cookbook right now, since it appears to be down). That way, you could
keep all references to the proxy, and change the encapsulated data when
you need it.

--
Ciao,
Matteo


这篇关于用泡菜更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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