__reduce__和__reduce_ex__有什么区别?

时间:2020-03-06 14:53:32  来源:igfitidea点击:

我知道这些方法是用于酸洗/酸洗的,与reduce内置函数没有关系,但是两者之间有什么区别,为什么我们都需要两者?

解决方案

医生说

If provided, at pickling time
  __reduce__() will be called with no
  arguments, and it must return either a
  string or a tuple.

另一方面,

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.

Guido说,在握的那只手上,这是可以清理的区域。

__reduce_ex__是__reduce__应该但从未成为的东西。 __reduce_ex__的工作方式类似于__reduce__,但是通过了pickle协议。