__reduce__和__reduce_ex__有什么区别? [英] What is the difference between __reduce__ and __reduce_ex__?
问题描述
我知道这些方法是用于酸洗/酸洗的,与reduce内置函数无关,但是两者之间有什么区别,为什么我们需要两者?
I understand that these methods are for pickling/unpickling and have no relation to the reduce built-in function, but what's the difference between the 2 and why do we need both?
推荐答案
文档表示
如果提供的话,在酸洗时
__reduce__()
将被调用而没有 参数,并且它必须返回一个 字符串或元组.
If provided, at pickling time
__reduce__()
will be called with no arguments, and it must return either a string or a tuple.
另一方面
有时候了解 实施时的协议版本
__reduce__
.这可以通过 实现一个名为__reduce_ex__
代替__reduce__
.__reduce_ex__
,当它 存在,优先于__reduce__
(您仍然可以提供__reduce__
向后 兼容性).__reduce_ex__
方法将被单个调用 整数参数,协议 版本.
It is sometimes useful to know the protocol version when implementing
__reduce__
. This can be done by implementing a method named__reduce_ex__
instead of__reduce__
.__reduce_ex__
, when it exists, is called in preference over__reduce__
(you may still provide__reduce__
for backwards compatibility). The__reduce_ex__
method will be called with a single integer argument, the protocol version.
On the gripping hand, Guido says that this is an area that could be cleaned up.
这篇关于__reduce__和__reduce_ex__有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!