Document __reduce_ex__.
This commit is contained in:
parent
d378d4b7c3
commit
10b912421d
42
pep-0307.txt
42
pep-0307.txt
|
@ -141,18 +141,19 @@ Extended __reduce__ API
|
|||
__setstate__; but the most powerful one is __reduce__. (There's
|
||||
also __getinitargs__, and we're adding __getnewargs__ below.)
|
||||
|
||||
There are two ways to provide __reduce__ functionality: a class
|
||||
can implement a __reduce__ method, or a reduce function can be
|
||||
declared in copy_reg (copy_reg.dispatch_table maps classes to
|
||||
functions). The return values are interpreted exactly the same,
|
||||
though, and we'll refer to these collectively as __reduce__.
|
||||
There are several ways to provide __reduce__ functionality: a
|
||||
class can implement a __reduce__ method or a __reduce_ex__ method
|
||||
(see next section), or a reduce function can be declared in
|
||||
copy_reg (copy_reg.dispatch_table maps classes to functions). The
|
||||
return values are interpreted exactly the same, though, and we'll
|
||||
refer to these collectively as __reduce__.
|
||||
|
||||
IMPORTANT: while a classic class can implement a __reduce__()
|
||||
method, pickling its instances ignores the method, so that a classic
|
||||
class cannot provide __reduce__ functionality in the sense intended
|
||||
here. (The copy_reg dispatch table is not consulted for classic
|
||||
classes either.) A classic class must use __getinitargs__ and/or
|
||||
__gestate__ to customize pickling. These are described below.
|
||||
IMPORTANT: pickling of classic class instances does not look for a
|
||||
__reduce__ or __reduce_ex__ method or a reduce function in the
|
||||
copy_reg dispatch table, so that a classic class cannot provide
|
||||
__reduce__ functionality in the sense intended here. A classic
|
||||
class must use __getinitargs__ and/or __gestate__ to customize
|
||||
pickling. These are described below.
|
||||
|
||||
__reduce__ must return either a string or a tuple. If it returns
|
||||
a string, this is an object whose state is not to be pickled, but
|
||||
|
@ -245,6 +246,22 @@ Extended __reduce__ API
|
|||
state (how this is done is up to the application).
|
||||
|
||||
|
||||
The __reduce_ex__ API
|
||||
|
||||
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.
|
||||
|
||||
The 'object' class implements both __reduce__ and __reduce_ex__;
|
||||
however, if a subclass overrides __reduce__ but not __reduce_ex__,
|
||||
the __reduce_ex__ implementation detects this and calls
|
||||
__reduce__.
|
||||
|
||||
|
||||
Customizing pickling absent a __reduce__ implementation
|
||||
|
||||
If no __reduce__ implementation is available for a particular
|
||||
|
@ -662,6 +679,9 @@ The copy module
|
|||
|
||||
In Python 2.3, several changes are made to the copy module:
|
||||
|
||||
- __reduce_ex__ is supported (and always called with 2 as the
|
||||
protocol version argument).
|
||||
|
||||
- The four- and five-argument return values of __reduce__ are
|
||||
supported.
|
||||
|
||||
|
|
Loading…
Reference in New Issue