mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Port changes to pickle docs apparently lost in py3k.
This commit is contained in:
parent
8527126f85
commit
c8148265dc
1 changed files with 74 additions and 68 deletions
|
@ -427,33 +427,38 @@ implementation of this behaviour::
|
||||||
obj.__dict__.update(attributes)
|
obj.__dict__.update(attributes)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
.. index:: single: __getnewargs__() (copy protocol)
|
|
||||||
|
|
||||||
Classes can alter the default behaviour by providing one or several special
|
Classes can alter the default behaviour by providing one or several special
|
||||||
methods. In protocol 2 and newer, classes that implements the
|
methods:
|
||||||
:meth:`__getnewargs__` method can dictate the values passed to the
|
|
||||||
:meth:`__new__` method upon unpickling. This is often needed for classes
|
|
||||||
whose :meth:`__new__` method requires arguments.
|
|
||||||
|
|
||||||
.. index:: single: __getstate__() (copy protocol)
|
.. method:: object.__getnewargs__()
|
||||||
|
|
||||||
Classes can further influence how their instances are pickled; if the class
|
In protocol 2 and newer, classes that implements the :meth:`__getnewargs__`
|
||||||
defines the method :meth:`__getstate__`, it is called and the returned object is
|
method can dictate the values passed to the :meth:`__new__` method upon
|
||||||
pickled as the contents for the instance, instead of the contents of the
|
unpickling. This is often needed for classes whose :meth:`__new__` method
|
||||||
instance's dictionary. If the :meth:`__getstate__` method is absent, the
|
requires arguments.
|
||||||
instance's :attr:`__dict__` is pickled as usual.
|
|
||||||
|
|
||||||
.. index:: single: __setstate__() (copy protocol)
|
|
||||||
|
|
||||||
Upon unpickling, if the class defines :meth:`__setstate__`, it is called with
|
.. method:: object.__getstate__()
|
||||||
the unpickled state. In that case, there is no requirement for the state object
|
|
||||||
to be a dictionary. Otherwise, the pickled state must be a dictionary and its
|
|
||||||
items are assigned to the new instance's dictionary.
|
|
||||||
|
|
||||||
.. note::
|
Classes can further influence how their instances are pickled; if the class
|
||||||
|
defines the method :meth:`__getstate__`, it is called and the returned object
|
||||||
|
is pickled as the contents for the instance, instead of the contents of the
|
||||||
|
instance's dictionary. If the :meth:`__getstate__` method is absent, the
|
||||||
|
instance's :attr:`__dict__` is pickled as usual.
|
||||||
|
|
||||||
|
|
||||||
|
.. method:: object.__setstate__(state)
|
||||||
|
|
||||||
|
Upon unpickling, if the class defines :meth:`__setstate__`, it is called with
|
||||||
|
the unpickled state. In that case, there is no requirement for the state
|
||||||
|
object to be a dictionary. Otherwise, the pickled state must be a dictionary
|
||||||
|
and its items are assigned to the new instance's dictionary.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
If :meth:`__getstate__` returns a false value, the :meth:`__setstate__`
|
||||||
|
method will not be called upon unpickling.
|
||||||
|
|
||||||
If :meth:`__getstate__` returns a false value, the :meth:`__setstate__`
|
|
||||||
method will not be called.
|
|
||||||
|
|
||||||
Refer to the section :ref:`pickle-state` for more information about how to use
|
Refer to the section :ref:`pickle-state` for more information about how to use
|
||||||
the methods :meth:`__getstate__` and :meth:`__setstate__`.
|
the methods :meth:`__getstate__` and :meth:`__setstate__`.
|
||||||
|
@ -462,14 +467,12 @@ the methods :meth:`__getstate__` and :meth:`__setstate__`.
|
||||||
|
|
||||||
At unpickling time, some methods like :meth:`__getattr__`,
|
At unpickling time, some methods like :meth:`__getattr__`,
|
||||||
:meth:`__getattribute__`, or :meth:`__setattr__` may be called upon the
|
:meth:`__getattribute__`, or :meth:`__setattr__` may be called upon the
|
||||||
instance. In case those methods rely on some internal invariant being
|
instance. In case those methods rely on some internal invariant being true,
|
||||||
true, the type should implement either :meth:`__getinitargs__` or
|
the type should implement :meth:`__getnewargs__` to establish such an
|
||||||
:meth:`__getnewargs__` to establish such an invariant; otherwise, neither
|
invariant; otherwise, neither :meth:`__new__` nor :meth:`__init__` will be
|
||||||
:meth:`__new__` nor :meth:`__init__` will be called.
|
called.
|
||||||
|
|
||||||
.. index::
|
.. index:: pair: copy; protocol
|
||||||
pair: copy; protocol
|
|
||||||
single: __reduce__() (copy protocol)
|
|
||||||
|
|
||||||
As we shall see, pickle does not use directly the methods described above. In
|
As we shall see, pickle does not use directly the methods described above. In
|
||||||
fact, these methods are part of the copy protocol which implements the
|
fact, these methods are part of the copy protocol which implements the
|
||||||
|
@ -480,58 +483,61 @@ objects. [#]_
|
||||||
Although powerful, implementing :meth:`__reduce__` directly in your classes is
|
Although powerful, implementing :meth:`__reduce__` directly in your classes is
|
||||||
error prone. For this reason, class designers should use the high-level
|
error prone. For this reason, class designers should use the high-level
|
||||||
interface (i.e., :meth:`__getnewargs__`, :meth:`__getstate__` and
|
interface (i.e., :meth:`__getnewargs__`, :meth:`__getstate__` and
|
||||||
:meth:`__setstate__`) whenever possible. We will show, however, cases where using
|
:meth:`__setstate__`) whenever possible. We will show, however, cases where
|
||||||
:meth:`__reduce__` is the only option or leads to more efficient pickling or
|
using :meth:`__reduce__` is the only option or leads to more efficient pickling
|
||||||
both.
|
or both.
|
||||||
|
|
||||||
The interface is currently defined as follows. The :meth:`__reduce__` method
|
.. method:: object.__reduce__()
|
||||||
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
|
The interface is currently defined as follows. The :meth:`__reduce__` method
|
||||||
global variable. It should be the object's local name relative to its module;
|
takes no argument and shall return either a string or preferably a tuple (the
|
||||||
the pickle module searches the module namespace to determine the object's
|
returned object is often referred to as the "reduce value").
|
||||||
module. This behaviour is typically useful for singletons.
|
|
||||||
|
|
||||||
When a tuple is returned, it must be between two and five items long. Optional
|
If a string is returned, the string should be interpreted as the name of a
|
||||||
items can either be omitted, or ``None`` can be provided as their value. The
|
global variable. It should be the object's local name relative to its
|
||||||
semantics of each item are in order:
|
module; the pickle module searches the module namespace to determine the
|
||||||
|
object's module. This behaviour is typically useful for singletons.
|
||||||
|
|
||||||
.. XXX Mention __newobj__ special-case?
|
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
|
.. XXX Mention __newobj__ special-case?
|
||||||
object.
|
|
||||||
|
|
||||||
* A tuple of arguments for the callable object. An empty tuple must be given if
|
* A callable object that will be called to create the initial version of the
|
||||||
the callable does not accept any argument.
|
object.
|
||||||
|
|
||||||
* Optionally, the object's state, which will be passed to the object's
|
* A tuple of arguments for the callable object. An empty tuple must be given
|
||||||
:meth:`__setstate__` method as previously described. If the object has no
|
if the callable does not accept any argument.
|
||||||
such method then, the value must be a dictionary and it will be added to the
|
|
||||||
object's :attr:`__dict__` attribute.
|
|
||||||
|
|
||||||
* Optionally, an iterator (and not a sequence) yielding successive items. These
|
* Optionally, the object's state, which will be passed to the object's
|
||||||
items will be appended to the object either using ``obj.append(item)`` or, in
|
:meth:`__setstate__` method as previously described. If the object has no
|
||||||
batch, using ``obj.extend(list_of_items)``. This is primarily used for list
|
such method then, the value must be a dictionary and it will be added to
|
||||||
subclasses, but may be used by other classes as long as they have
|
the object's :attr:`__dict__` attribute.
|
||||||
:meth:`append` and :meth:`extend` methods with the appropriate signature.
|
|
||||||
(Whether :meth:`append` or :meth:`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.
|
* Optionally, an iterator (and not a sequence) yielding successive items.
|
||||||
These items will be stored to the object using ``obj[key] = value``. This is
|
These items will be appended to the object either using
|
||||||
primarily used for dictionary subclasses, but may be used by other classes as
|
``obj.append(item)`` or, in batch, using ``obj.extend(list_of_items)``.
|
||||||
long as they implement :meth:`__setitem__`.
|
This is primarily used for list subclasses, but may be used by other
|
||||||
|
classes as long as they have :meth:`append` and :meth:`extend` methods with
|
||||||
|
the appropriate signature. (Whether :meth:`append` or :meth:`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.)
|
||||||
|
|
||||||
.. index:: single: __reduce_ex__() (copy protocol)
|
* 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 :meth:`__setitem__`.
|
||||||
|
|
||||||
Alternatively, a :meth:`__reduce_ex__` method may be defined. The only
|
|
||||||
difference is this method should take a single integer argument, the protocol
|
.. method:: object.__reduce_ex__(protocol)
|
||||||
version. When defined, pickle will prefer it over the :meth:`__reduce__`
|
|
||||||
method. In addition, :meth:`__reduce__` automatically becomes a synonym for the
|
Alternatively, a :meth:`__reduce_ex__` method may be defined. The only
|
||||||
extended version. The main use for this method is to provide
|
difference is this method should take a single integer argument, the protocol
|
||||||
backwards-compatible reduce values for older Python releases.
|
version. When defined, pickle will prefer it over the :meth:`__reduce__`
|
||||||
|
method. In addition, :meth:`__reduce__` automatically becomes a synonym for
|
||||||
|
the extended version. The main use for this method is to provide
|
||||||
|
backwards-compatible reduce values for older Python releases.
|
||||||
|
|
||||||
.. _pickle-persistent:
|
.. _pickle-persistent:
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue