sách gpt4 ăn đã đi

python - 如何使用自引用和带有插槽的类来 pickle 和取消 pickle 对象?

In lại 作者:太空狗 更新时间:2023-10-29 21:51:09 27 4
mua khóa gpt4 giày nike

当对象通过其属性之一引用自身时,从带有插槽的类中挑选对象的正确方法是什么?这是一个简单的示例,使用我当前的实现,我不确定它是否 100% 正确:

import weakref
import pickle

class my_class(object):

__slots__ = ('an_int', 'ref_to_self', '__weakref__')

định nghĩa __init__(bản thân):
self.an_int = 42
self.ref_to_self = weakref.WeakKeyDictionary({self: 1})

# How to best write __getstate__ and __setstate__?
def __getstate__(self):

obj_slot_values = dict((k, getattr(self, k)) for k in self.__slots__)
# Conversion to a usual dictionary:
obj_slot_values['ref_to_self'] = dict(obj_slot_values['ref_to_self'])
# Unpicklable weakref object:
del obj_slot_values['__weakref__']
return obj_slot_values

def __setstate__(self, data_dict):
# print data_dict
for (name, value) in data_dict.iteritems():
setattr(self, tên, giá trị)
# Conversion of the dict back to a WeakKeyDictionary:
self.ref_to_self = weakref.WeakKeyDictionary(
self.ref_to_self.iteritems())

这可以通过以下方式进行测试:

def test_pickling(obj):
"Pickles obj and unpickles it. Returns the unpickled object"

obj_pickled = pickle.dumps(obj)
obj_unpickled = pickle.loads(obj_pickled)

# Self-references should be kept:
print "OK?", obj_unpickled == obj_unpickled.ref_to_self.keys()[0]
print "OK?", isinstance(obj_unpickled.ref_to_self,
weakref.WeakKeyDictionary)

return obj_unpickled

nếu __name__ == '__main__':
obj = my_class()
obj_unpickled = test_pickling(obj)
obj_unpickled2 = test_pickling(obj_unpickled)

这是一个正确/稳健的实现吗?如果my_class继承自一个带有__slots__的类,__getstate____setstate__应该怎么写? __setstate__ 是否因为“循环”命令而发生内存泄漏?

PEP 307中有备注这让我想知道 pickling my_class 对象是否完全有可能以一种可靠的方式:

Các __getstate__ method should return a picklable value representing the object's state without referencing the object itself.

这是否与对对象本身的引用被 pickle 的事实冲突?

问题很多:任何评论、评论或建议都将不胜感激!

câu trả lời hay nhất

看起来原始帖子建议的效果很好。

至于 PEP 307 的内容:

The __getstate__ method should return a picklable value representing the object's state without referencing the object itself.

我理解这仅意味着 __getstate__ 方法必须简单地返回一个不指向(unpickleable)原始对象的表示。因此,返回一个引用自身的对象是可以的,只要没有对原始(unpickleable)对象的引用。

关于python - 如何使用自引用和带有插槽的类来 pickle 和取消 pickle 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2922628/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress