@@ -352,7 +352,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
352
352
:func: `copyreg.pickle `. It is a mapping whose keys are classes
353
353
and whose values are reduction functions. A reduction function
354
354
takes a single argument of the associated class and should
355
- conform to the same interface as a :meth: `__reduce__ `
355
+ conform to the same interface as a :meth: `~object. __reduce__ `
356
356
method.
357
357
358
358
By default, a pickler object will not have a
@@ -372,7 +372,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
372
372
373
373
Special reducer that can be defined in :class: `Pickler ` subclasses. This
374
374
method has priority over any reducer in the :attr: `dispatch_table `. It
375
- should conform to the same interface as a :meth: `__reduce__ ` method, and
375
+ should conform to the same interface as a :meth: `~object. __reduce__ ` method, and
376
376
can optionally return ``NotImplemented `` to fallback on
377
377
:attr: `dispatch_table `-registered reducers to pickle ``obj ``.
378
378
@@ -508,7 +508,7 @@ The following types can be pickled:
508
508
509
509
* classes accessible from the top level of a module;
510
510
511
- * instances of such classes whose the result of calling :meth: `__getstate__ `
511
+ * instances of such classes whose the result of calling :meth: `~object. __getstate__ `
512
512
is picklable (see section :ref: `pickle-inst ` for details).
513
513
514
514
Attempts to pickle unpicklable objects will raise the :exc: `PicklingError `
@@ -544,7 +544,7 @@ purpose, so you can fix bugs in a class or add methods to the class and still
544
544
load objects that were created with an earlier version of the class. If you
545
545
plan to have long-lived objects that will see many versions of a class, it may
546
546
be worthwhile to put a version number in the objects so that suitable
547
- conversions can be made by the class's :meth: `__setstate__ ` method.
547
+ conversions can be made by the class's :meth: `~object. __setstate__ ` method.
548
548
549
549
550
550
.. _pickle-inst :
@@ -559,7 +559,7 @@ customize, and control how class instances are pickled and unpickled.
559
559
560
560
In most cases, no additional code is needed to make instances picklable. By
561
561
default, pickle will retrieve the class and the attributes of an instance via
562
- introspection. When a class instance is unpickled, its :meth: `__init__ ` method
562
+ introspection. When a class instance is unpickled, its :meth: `~object. __init__ ` method
563
563
is usually *not * invoked. The default behaviour first creates an uninitialized
564
564
instance and then restores the saved attributes. The following code shows an
565
565
implementation of this behaviour::
@@ -650,30 +650,30 @@ methods:
650
650
651
651
652
652
Refer to the section :ref: `pickle-state ` for more information about how to use
653
- the methods :meth: `__getstate__ ` and :meth: `__setstate__ `.
653
+ the methods :meth: `~object. __getstate__ ` and :meth: `~object. __setstate__ `.
654
654
655
655
.. note ::
656
656
657
- At unpickling time, some methods like :meth: `__getattr__ `,
658
- :meth: `__getattribute__ `, or :meth: `__setattr__ ` may be called upon the
657
+ At unpickling time, some methods like :meth: `~object. __getattr__ `,
658
+ :meth: `~object. __getattribute__ `, or :meth: `~object. __setattr__ ` may be called upon the
659
659
instance. In case those methods rely on some internal invariant being
660
- true, the type should implement :meth: `__new__ ` to establish such an
661
- invariant, as :meth: `__init__ ` is not called when unpickling an
660
+ true, the type should implement :meth: `~object. __new__ ` to establish such an
661
+ invariant, as :meth: `~object. __init__ ` is not called when unpickling an
662
662
instance.
663
663
664
664
.. index :: pair: copy; protocol
665
665
666
666
As we shall see, pickle does not use directly the methods described above. In
667
667
fact, these methods are part of the copy protocol which implements the
668
- :meth: `__reduce__ ` special method. The copy protocol provides a unified
668
+ :meth: `~object. __reduce__ ` special method. The copy protocol provides a unified
669
669
interface for retrieving the data necessary for pickling and copying
670
670
objects. [# ]_
671
671
672
- Although powerful, implementing :meth: `__reduce__ ` directly in your classes is
672
+ Although powerful, implementing :meth: `~object. __reduce__ ` directly in your classes is
673
673
error prone. For this reason, class designers should use the high-level
674
- interface (i.e., :meth: `__getnewargs_ex__ `, :meth: `__getstate__ ` and
675
- :meth: `__setstate__ `) whenever possible. We will show, however, cases where
676
- using :meth: `__reduce__ ` is the only option or leads to more efficient pickling
674
+ interface (i.e., :meth: `~object. __getnewargs_ex__ `, :meth: `~object. __getstate__ ` and
675
+ :meth: `~object. __setstate__ `) whenever possible. We will show, however, cases where
676
+ using :meth: `! __reduce__ ` is the only option or leads to more efficient pickling
677
677
or both.
678
678
679
679
.. method :: object.__reduce__()
@@ -708,8 +708,9 @@ or both.
708
708
These items will be appended to the object either using
709
709
``obj.append(item) `` or, in batch, using ``obj.extend(list_of_items) ``.
710
710
This is primarily used for list subclasses, but may be used by other
711
- classes as long as they have :meth: `append ` and :meth: `extend ` methods with
712
- the appropriate signature. (Whether :meth: `append ` or :meth: `extend ` is
711
+ classes as long as they have
712
+ :ref: `append and extend methods <typesseq-common >` with
713
+ the appropriate signature. (Whether :meth: `!append ` or :meth: `!extend ` is
713
714
used depends on which pickle protocol version is used as well as the number
714
715
of items to append, so both must be supported.)
715
716
@@ -785,8 +786,8 @@ any other code which depends on pickling, then one can create a
785
786
pickler with a private dispatch table.
786
787
787
788
The global dispatch table managed by the :mod: `copyreg ` module is
788
- available as :data: `copyreg.dispatch_table `. Therefore, one may
789
- choose to use a modified copy of :data: `copyreg.dispatch_table ` as a
789
+ available as :data: `! copyreg.dispatch_table `. Therefore, one may
790
+ choose to use a modified copy of :data: `! copyreg.dispatch_table ` as a
790
791
private dispatch table.
791
792
792
793
For example ::
@@ -825,12 +826,12 @@ Handling Stateful Objects
825
826
single: __setstate__() (copy protocol)
826
827
827
828
Here's an example that shows how to modify pickling behavior for a class.
828
- The :class: `TextReader ` class opens a text file, and returns the line number and
829
+ The :class: `! TextReader ` class below opens a text file, and returns the line number and
829
830
line contents each time its :meth: `!readline ` method is called. If a
830
- :class: `TextReader ` instance is pickled, all attributes *except * the file object
831
+ :class: `! TextReader ` instance is pickled, all attributes *except * the file object
831
832
member are saved. When the instance is unpickled, the file is reopened, and
832
- reading resumes from the last location. The :meth: `__setstate__ ` and
833
- :meth: `__getstate__ ` methods are used to implement this behavior. ::
833
+ reading resumes from the last location. The :meth: `! __setstate__ ` and
834
+ :meth: `! __getstate__ ` methods are used to implement this behavior. ::
834
835
835
836
class TextReader:
836
837
"""Print and number lines in a text file."""
@@ -895,7 +896,7 @@ functions and classes.
895
896
896
897
For those cases, it is possible to subclass from the :class: `Pickler ` class and
897
898
implement a :meth: `~Pickler.reducer_override ` method. This method can return an
898
- arbitrary reduction tuple (see :meth: `__reduce__ `). It can alternatively return
899
+ arbitrary reduction tuple (see :meth: `~object. __reduce__ `). It can alternatively return
899
900
``NotImplemented `` to fallback to the traditional behavior.
900
901
901
902
If both the :attr: `~Pickler.dispatch_table ` and
@@ -963,7 +964,7 @@ provided by pickle protocol 5 and higher.
963
964
Provider API
964
965
^^^^^^^^^^^^
965
966
966
- The large data objects to be pickled must implement a :meth: `__reduce_ex__ `
967
+ The large data objects to be pickled must implement a :meth: `~object. __reduce_ex__ `
967
968
method specialized for protocol 5 and higher, which returns a
968
969
:class: `PickleBuffer ` instance (instead of e.g. a :class: `bytes ` object)
969
970
for any large data.
0 commit comments