object.__reduce__() The interface is currently defined as follows. The __reduce__() method takes no argument and shall return either a string or preferably a tuple (the returned object is often referred to as the “reduce value”). 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 six 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__(). Optionally, a callable with a (obj, state) signature. This callable allows the user to programmatically control the state-updating behavior of a specific object, instead of using obj’s static __setstate__() method. If not None, this callable will have priority over obj’s __setstate__(). New in version 3.8: The optional sixth tuple item, (obj, state), was added. 接口当前定义如下。__reduce__()方法不接受参数,返回一个字符串,最好是一个元组(返回的对象通常被称为“reduce值”)。 如果返回的是字符串,则应该将该字符串解释为全局变量的名称。它应该是对象相对于其模块的局部名称;pickle模块通过搜索模块命名空间来确定对象所属的模块。这种行为通常对单例很有用。 返回元组时,它的长度必须在2到6个元素之间。可选项可以省略,也可以赋值None。每个元素项的语义如下: 一个可调用对象,将被调用以创建该对象的初始版本。 可调用对象的参数元组。如果可调用对象不接受任何参数,则必须给出空元组。 对象的状态是可选的,它将像之前描述的那样传递给对象的__setstate__()方法。如果对象没有这样的方法,则值必须是一个字典,并将其添加到对象的__dict__属性中。 可选的,产生连续元素的迭代器(而不是序列)。这些项可以使用obj.append(item)添加到对象中,也可以使用obj.extend(list_of_items)批量添加。这主要用于列表的子类,但其他类也可以使用,只要添加和扩展的方法具有适当的签名。(使用append()还是extend()取决于使用的pickle协议版本以及要追加的项的数量,因此必须同时支持这两种协议。) 一个可选的迭代器(不是序列),产生连续的键值对。使用obj[key] = value将这些项存储到对象中。这主要用于字典的子类,但其他类也可以使用,只要它们实现了__setitem__()。 可选的,带有(obj, state)签名的可调用对象。这个可调用方法允许用户以编程方式控制特定对象的状态更新行为,而不是使用obj的静态__setstate__()方法。如果不是None,这个可调用对象将优先于obj的__setstate__()。 3.8新版功能:添加了可选的第六个元组项(obj, state)。