@@ -147,7 +147,7 @@ to read the pickle produced.
147
147
earlier versions of Python.
148
148
149
149
* Protocol version 2 was introduced in Python 2.3. It provides much more
150
- efficient pickling of :term: `new-style class ` \e s . Refer to :pep: `307 ` for
150
+ efficient pickling of :term: `new-style classes <new-style class> ` . Refer to :pep: `307 ` for
151
151
information about improvements brought by protocol 2.
152
152
153
153
* Protocol version 3 was added in Python 3.0. It has explicit support for
@@ -261,7 +261,7 @@ process more convenient:
261
261
protocol argument is needed. Bytes past the pickled representation
262
262
of the object are ignored.
263
263
264
- Arguments *file *, * fix_imports *, *encoding *, *errors *, *strict * and *buffers *
264
+ Arguments *fix_imports *, *encoding *, *errors *, *strict * and *buffers *
265
265
have the same meaning as in the :class: `Unpickler ` constructor.
266
266
267
267
.. versionchanged :: 3.8
@@ -368,7 +368,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
368
368
369
369
.. versionadded :: 3.3
370
370
371
- .. method :: reducer_override(self, obj)
371
+ .. method :: reducer_override(obj)
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
@@ -494,20 +494,18 @@ What can be pickled and unpickled?
494
494
495
495
The following types can be pickled:
496
496
497
- * ``None ``, ``True ``, and ``False ``
498
-
499
- * integers, floating point numbers, complex numbers
497
+ * ``None ``, ``True ``, and ``False ``;
500
498
501
- * strings, bytes, bytearrays
499
+ * integers, floating-point numbers, complex numbers;
502
500
503
- * tuples, lists, sets, and dictionaries containing only picklable objects
501
+ * strings, bytes, bytearrays;
504
502
505
- * functions defined at the top level of a module (using :keyword: `def `, not
506
- :keyword: `lambda `)
503
+ * tuples, lists, sets, and dictionaries containing only picklable objects;
507
504
508
- * built-in functions defined at the top level of a module
505
+ * functions (built-in and user-defined) defined at the top level of a module
506
+ (using :keyword: `def `, not :keyword: `lambda `);
509
507
510
- * classes that are defined at the top level of a module
508
+ * classes defined at the top level of a module;
511
509
512
510
* instances of such classes whose the result of calling :meth: `__getstate__ `
513
511
is picklable (see section :ref: `pickle-inst ` for details).
@@ -519,14 +517,14 @@ structure may exceed the maximum recursion depth, a :exc:`RecursionError` will b
519
517
raised in this case. You can carefully raise this limit with
520
518
:func: `sys.setrecursionlimit `.
521
519
522
- Note that functions (built-in and user-defined) are pickled by " fully qualified"
523
- name reference , not by value. [# ]_ This means that only the function name is
520
+ Note that functions (built-in and user-defined) are pickled by fully qualified
521
+ name, not by value. [# ]_ This means that only the function name is
524
522
pickled, along with the name of the module the function is defined in. Neither
525
523
the function's code, nor any of its function attributes are pickled. Thus the
526
524
defining module must be importable in the unpickling environment, and the module
527
525
must contain the named object, otherwise an exception will be raised. [# ]_
528
526
529
- Similarly, classes are pickled by named reference , so the same restrictions in
527
+ Similarly, classes are pickled by fully qualified name , so the same restrictions in
530
528
the unpickling environment apply. Note that none of the class's code or data is
531
529
pickled, so in the following example the class attribute ``attr `` is not
532
530
restored in the unpickling environment::
@@ -536,7 +534,7 @@ restored in the unpickling environment::
536
534
537
535
picklestring = pickle.dumps(Foo)
538
536
539
- These restrictions are why picklable functions and classes must be defined in
537
+ These restrictions are why picklable functions and classes must be defined at
540
538
the top level of a module.
541
539
542
540
Similarly, when class instances are pickled, their class's code and data are not
@@ -568,7 +566,7 @@ implementation of this behaviour::
568
566
def save(obj):
569
567
return (obj.__class__, obj.__dict__)
570
568
571
- def load (cls, attributes):
569
+ def restore (cls, attributes):
572
570
obj = cls.__new__(cls)
573
571
obj.__dict__.update(attributes)
574
572
return obj
@@ -807,14 +805,15 @@ the code ::
807
805
f = io.BytesIO()
808
806
p = MyPickler(f)
809
807
810
- does the same, but all instances of ``MyPickler `` will by default
811
- share the same dispatch table. The equivalent code using the
812
- :mod: `copyreg ` module is ::
808
+ does the same but all instances of ``MyPickler `` will by default
809
+ share the private dispatch table. On the other hand, the code ::
813
810
814
811
copyreg.pickle(SomeClass, reduce_SomeClass)
815
812
f = io.BytesIO()
816
813
p = pickle.Pickler(f)
817
814
815
+ modifies the global dispatch table shared by all users of the :mod: `copyreg ` module.
816
+
818
817
.. _pickle-state :
819
818
820
819
Handling Stateful Objects
@@ -1117,7 +1116,7 @@ Here is an example of an unpickler allowing only few safe classes from the
1117
1116
"""Helper function analogous to pickle.loads()."""
1118
1117
return RestrictedUnpickler(io.BytesIO(s)).load()
1119
1118
1120
- A sample usage of our unpickler working has intended::
1119
+ A sample usage of our unpickler working as intended::
1121
1120
1122
1121
>>> restricted_loads(pickle.dumps([1, 2, range(15)]))
1123
1122
[1, 2, range(0, 15)]
@@ -1161,7 +1160,7 @@ For the simplest code, use the :func:`dump` and :func:`load` functions. ::
1161
1160
1162
1161
# An arbitrary collection of objects supported by pickle.
1163
1162
data = {
1164
- 'a': [1, 2.0, 3, 4+6j ],
1163
+ 'a': [1, 2.0, 3+4j ],
1165
1164
'b': ("character string", b"byte string"),
1166
1165
'c': {None, True, False}
1167
1166
}
@@ -1217,6 +1216,6 @@ The following example reads the resulting pickled data. ::
1217
1216
operations.
1218
1217
1219
1218
.. [# ] The limitation on alphanumeric characters is due to the fact
1220
- the persistent IDs, in protocol 0, are delimited by the newline
1219
+ that persistent IDs in protocol 0 are delimited by the newline
1221
1220
character. Therefore if any kind of newline characters occurs in
1222
- persistent IDs, the resulting pickle will become unreadable.
1221
+ persistent IDs, the resulting pickled data will become unreadable.
0 commit comments