gpt4 book ai didi

python - __reduce__ 函数在 pickle 模块的情况下如何工作?

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:14 24 4
gpt4 key购买 nike

我不明白 __reduce__ 函数在 Python 中的 pickle 模块中是如何工作的。

假设我有以下类(class):

class Foo(object):
def __init__(self, file_name = 'file.txt'):
self.file_name = file_name
self.f = open(self.file_name, 'w')

它不能被 pickle 因为 pickle 模块不知道如何编码文件句柄:

foo = Foo()
print(pickle.dumps(foo))

输出:

TypeError: can't pickle file objects

但如果我添加 __reduce__ 函数,它会成功编码:

import pickle

class Foo(object):
def __init__(self, file_name = 'file.txt'):
self.file_name = file_name
self.f = open(self.file_name, 'w')

def __reduce__(self):
return (self.__class__, (self.file_name, ))

foo = Foo()
print(pickle.dumps(foo))

输出:

c__main__
Foo
p0
(S'file.txt'
p1
tp2
Rp3
.

如果 pickle.dumps 调用失败,__reduce__ 函数只是返回解构器重新创建原始对象的“指令”,我说得对吗?

我从文档中不清楚。

最佳答案

你是对的。 __reduce__ 方法应该返回如何重建(unpickle)对象的提示,以防它不能被自动 pickle。它可能包含一个对象引用和参数,将调用它来创建对象的初始版本、对象的状态等。

来自documentation :

If a string is returned, the string should be interpreted as the name of a global variable. It should be the object’s local name relative to its module; the pickle module searches the module namespace to determine the object’s module. This behaviour is typically useful for singletons.

When a tuple is returned, it must be between two and five items long. Optional items can either be omitted, or None can be provided as their value. The semantics of each item are in order:

  • A callable object that will be called to create the initial version of the object.
  • A tuple of arguments for the callable object. An empty tuple must be given if the callable does not accept any argument.
  • Optionally, the object’s state, which will be passed to the object’s __setstate__() method as previously described. If the object has no such method then, the value must be a dictionary and it will be added to the object’s __dict__ attribute.
  • Optionally, an iterator (and not a sequence) yielding successive items. These items will be appended to the object either using obj.append(item) or, in batch, using obj.extend(list_of_items). This is primarily used for list subclasses, but may be used by other classes as long as they have append() and extend() methods with the appropriate signature. (Whether append() or extend() is used depends on which pickle protocol version is used as well as the number of items to append, so both must be supported.)
  • Optionally, an iterator (not a sequence) yielding successive key-value pairs. These items will be stored to the object using obj[key] = value. This is primarily used for dictionary subclasses, but may be used by other classes as long as they implement __setitem__().

关于python - __reduce__ 函数在 pickle 模块的情况下如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38813206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com