@@ -356,7 +356,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
356
356
:func: `copyreg.pickle `. It is a mapping whose keys are classes
357
357
and whose values are reduction functions. A reduction function
358
358
takes a single argument of the associated class and should
359
- conform to the same interface as a :meth: `__reduce__ `
359
+ conform to the same interface as a :meth: `~object. __reduce__ `
360
360
method.
361
361
362
362
By default, a pickler object will not have a
@@ -376,7 +376,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
376
376
377
377
Special reducer that can be defined in :class: `Pickler ` subclasses. This
378
378
method has priority over any reducer in the :attr: `dispatch_table `. It
379
- should conform to the same interface as a :meth: `__reduce__ ` method, and
379
+ should conform to the same interface as a :meth: `~object. __reduce__ ` method, and
380
380
can optionally return ``NotImplemented `` to fallback on
381
381
:attr: `dispatch_table `-registered reducers to pickle ``obj ``.
382
382
@@ -516,7 +516,7 @@ The following types can be pickled:
516
516
517
517
* classes accessible from the top level of a module;
518
518
519
- * instances of such classes whose the result of calling :meth: `__getstate__ `
519
+ * instances of such classes whose the result of calling :meth: `~object. __getstate__ `
520
520
is picklable (see section :ref: `pickle-inst ` for details).
521
521
522
522
Attempts to pickle unpicklable objects will raise the :exc: `PicklingError `
@@ -552,7 +552,7 @@ purpose, so you can fix bugs in a class or add methods to the class and still
552
552
load objects that were created with an earlier version of the class. If you
553
553
plan to have long-lived objects that will see many versions of a class, it may
554
554
be worthwhile to put a version number in the objects so that suitable
555
- conversions can be made by the class's :meth: `__setstate__ ` method.
555
+ conversions can be made by the class's :meth: `~object. __setstate__ ` method.
556
556
557
557
558
558
.. _pickle-inst :
@@ -567,7 +567,7 @@ customize, and control how class instances are pickled and unpickled.
567
567
568
568
In most cases, no additional code is needed to make instances picklable. By
569
569
default, pickle will retrieve the class and the attributes of an instance via
570
- introspection. When a class instance is unpickled, its :meth: `__init__ ` method
570
+ introspection. When a class instance is unpickled, its :meth: `~object. __init__ ` method
571
571
is usually *not * invoked. The default behaviour first creates an uninitialized
572
572
instance and then restores the saved attributes. The following code shows an
573
573
implementation of this behaviour::
@@ -658,30 +658,30 @@ methods:
658
658
659
659
660
660
Refer to the section :ref: `pickle-state ` for more information about how to use
661
- the methods :meth: `__getstate__ ` and :meth: `__setstate__ `.
661
+ the methods :meth: `~object. __getstate__ ` and :meth: `~object. __setstate__ `.
662
662
663
663
.. note ::
664
664
665
- At unpickling time, some methods like :meth: `__getattr__ `,
666
- :meth: `__getattribute__ `, or :meth: `__setattr__ ` may be called upon the
665
+ At unpickling time, some methods like :meth: `~object. __getattr__ `,
666
+ :meth: `~object. __getattribute__ `, or :meth: `~object. __setattr__ ` may be called upon the
667
667
instance. In case those methods rely on some internal invariant being
668
- true, the type should implement :meth: `__new__ ` to establish such an
669
- invariant, as :meth: `__init__ ` is not called when unpickling an
668
+ true, the type should implement :meth: `~object. __new__ ` to establish such an
669
+ invariant, as :meth: `~object. __init__ ` is not called when unpickling an
670
670
instance.
671
671
672
672
.. index :: pair: copy; protocol
673
673
674
674
As we shall see, pickle does not use directly the methods described above. In
675
675
fact, these methods are part of the copy protocol which implements the
676
- :meth: `__reduce__ ` special method. The copy protocol provides a unified
676
+ :meth: `~object. __reduce__ ` special method. The copy protocol provides a unified
677
677
interface for retrieving the data necessary for pickling and copying
678
678
objects. [# ]_
679
679
680
- Although powerful, implementing :meth: `__reduce__ ` directly in your classes is
680
+ Although powerful, implementing :meth: `~object. __reduce__ ` directly in your classes is
681
681
error prone. For this reason, class designers should use the high-level
682
- interface (i.e., :meth: `__getnewargs_ex__ `, :meth: `__getstate__ ` and
683
- :meth: `__setstate__ `) whenever possible. We will show, however, cases where
684
- using :meth: `__reduce__ ` is the only option or leads to more efficient pickling
682
+ interface (i.e., :meth: `~object. __getnewargs_ex__ `, :meth: `~object. __getstate__ ` and
683
+ :meth: `~object. __setstate__ `) whenever possible. We will show, however, cases where
684
+ using :meth: `! __reduce__ ` is the only option or leads to more efficient pickling
685
685
or both.
686
686
687
687
.. method :: object.__reduce__()
@@ -716,8 +716,9 @@ or both.
716
716
These items will be appended to the object either using
717
717
``obj.append(item) `` or, in batch, using ``obj.extend(list_of_items) ``.
718
718
This is primarily used for list subclasses, but may be used by other
719
- classes as long as they have :meth: `append ` and :meth: `extend ` methods with
720
- the appropriate signature. (Whether :meth: `append ` or :meth: `extend ` is
719
+ classes as long as they have
720
+ :ref: `append and extend methods <typesseq-common >` with
721
+ the appropriate signature. (Whether :meth: `!append ` or :meth: `!extend ` is
721
722
used depends on which pickle protocol version is used as well as the number
722
723
of items to append, so both must be supported.)
723
724
@@ -793,8 +794,8 @@ any other code which depends on pickling, then one can create a
793
794
pickler with a private dispatch table.
794
795
795
796
The global dispatch table managed by the :mod: `copyreg ` module is
796
- available as :data: `copyreg.dispatch_table `. Therefore, one may
797
- choose to use a modified copy of :data: `copyreg.dispatch_table ` as a
797
+ available as :data: `! copyreg.dispatch_table `. Therefore, one may
798
+ choose to use a modified copy of :data: `! copyreg.dispatch_table ` as a
798
799
private dispatch table.
799
800
800
801
For example ::
@@ -833,12 +834,12 @@ Handling Stateful Objects
833
834
single: __setstate__() (copy protocol)
834
835
835
836
Here's an example that shows how to modify pickling behavior for a class.
836
- The :class: `TextReader ` class opens a text file, and returns the line number and
837
+ The :class: `! TextReader ` class below opens a text file, and returns the line number and
837
838
line contents each time its :meth: `!readline ` method is called. If a
838
- :class: `TextReader ` instance is pickled, all attributes *except * the file object
839
+ :class: `! TextReader ` instance is pickled, all attributes *except * the file object
839
840
member are saved. When the instance is unpickled, the file is reopened, and
840
- reading resumes from the last location. The :meth: `__setstate__ ` and
841
- :meth: `__getstate__ ` methods are used to implement this behavior. ::
841
+ reading resumes from the last location. The :meth: `! __setstate__ ` and
842
+ :meth: `! __getstate__ ` methods are used to implement this behavior. ::
842
843
843
844
class TextReader:
844
845
"""Print and number lines in a text file."""
@@ -903,7 +904,7 @@ functions and classes.
903
904
904
905
For those cases, it is possible to subclass from the :class: `Pickler ` class and
905
906
implement a :meth: `~Pickler.reducer_override ` method. This method can return an
906
- arbitrary reduction tuple (see :meth: `__reduce__ `). It can alternatively return
907
+ arbitrary reduction tuple (see :meth: `~object. __reduce__ `). It can alternatively return
907
908
``NotImplemented `` to fallback to the traditional behavior.
908
909
909
910
If both the :attr: `~Pickler.dispatch_table ` and
@@ -971,7 +972,7 @@ provided by pickle protocol 5 and higher.
971
972
Provider API
972
973
^^^^^^^^^^^^
973
974
974
- The large data objects to be pickled must implement a :meth: `__reduce_ex__ `
975
+ The large data objects to be pickled must implement a :meth: `~object. __reduce_ex__ `
975
976
method specialized for protocol 5 and higher, which returns a
976
977
:class: `PickleBuffer ` instance (instead of e.g. a :class: `bytes ` object)
977
978
for any large data.
0 commit comments