class pickle.Pickler(file, protocol=None, *, fix_imports=True)
This takes a binary file for writing a pickle data stream.
The optional protocol argument, an integer, tells the pickler to use the given protocol; supported protocols are 0 to HIGHEST_PROTOCOL
. If not specified, the default is DEFAULT_PROTOCOL
. If a negative number is specified, HIGHEST_PROTOCOL
is selected.
The file argument must have a write() method that accepts a single bytes argument. It can thus be an on-disk file opened for binary writing, an io.BytesIO
instance, or any other custom object that meets this interface.
If fix_imports is true and protocol is less than 3, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.
-
dump(obj)
-
Write a pickled representation of obj to the open file object given in the constructor.
-
persistent_id(obj)
-
Do nothing by default. This exists so a subclass can override it.
If
persistent_id()
returnsNone
, obj is pickled as usual. Any other value causesPickler
to emit the returned value as a persistent ID for obj. The meaning of this persistent ID should be defined byUnpickler.persistent_load()
. Note that the value returned bypersistent_id()
cannot itself have a persistent ID.See Persistence of External Objects for details and examples of uses.
-
dispatch_table
-
A pickler object’s dispatch table is a registry of reduction functions of the kind which can be declared using
copyreg.pickle()
. It is a mapping whose keys are classes and whose values are reduction functions. A reduction function takes a single argument of the associated class and should conform to the same interface as a__reduce__()
method.By default, a pickler object will not have a
dispatch_table
attribute, and it will instead use the global dispatch table managed by thecopyreg
module. However, to customize the pickling for a specific pickler object one can set thedispatch_table
attribute to a dict-like object. Alternatively, if a subclass ofPickler
has adispatch_table
attribute then this will be used as the default dispatch table for instances of that class.See Dispatch Tables for usage examples.
New in version 3.3.
-
fast
-
Deprecated. Enable fast mode if set to a true value. The fast mode disables the usage of memo, therefore speeding the pickling process by not generating superfluous PUT opcodes. It should not be used with self-referential objects, doing otherwise will cause
Pickler
to recurse infinitely.Use
pickletools.optimize()
if you need more compact pickles.
Please login to continue.