Linux server.nvwebsoft.co.in 3.10.0-1160.114.2.el7.x86_64 #1 SMP Wed Mar 20 15:54:52 UTC 2024 x86_64
Apache
: 162.240.12.249 | : 3.139.234.241
202 Domain
8.1.31
nbspublicschool
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
CREATE WP USER
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
49.27
KB
-rw-r--r--
__builtin__.html
10.26
KB
-rw-r--r--
__future__.html
13.79
KB
-rw-r--r--
__main__.html
7.05
KB
-rw-r--r--
_winreg.html
59.21
KB
-rw-r--r--
abc.html
23.9
KB
-rw-r--r--
aepack.html
13.16
KB
-rw-r--r--
aetools.html
14.91
KB
-rw-r--r--
aetypes.html
18.88
KB
-rw-r--r--
aifc.html
22.4
KB
-rw-r--r--
al.html
17.34
KB
-rw-r--r--
allos.html
33.72
KB
-rw-r--r--
anydbm.html
16.33
KB
-rw-r--r--
archiving.html
9.26
KB
-rw-r--r--
argparse.html
237.62
KB
-rw-r--r--
array.html
29.29
KB
-rw-r--r--
ast.html
34.98
KB
-rw-r--r--
asynchat.html
31.43
KB
-rw-r--r--
asyncore.html
36.51
KB
-rw-r--r--
atexit.html
16.8
KB
-rw-r--r--
audioop.html
31.36
KB
-rw-r--r--
autogil.html
8.19
KB
-rw-r--r--
base64.html
19.67
KB
-rw-r--r--
basehttpserver.html
34.04
KB
-rw-r--r--
bastion.html
11.04
KB
-rw-r--r--
bdb.html
36.68
KB
-rw-r--r--
binascii.html
20.67
KB
-rw-r--r--
binhex.html
10.58
KB
-rw-r--r--
bisect.html
23.24
KB
-rw-r--r--
bsddb.html
26.43
KB
-rw-r--r--
bz2.html
26.08
KB
-rw-r--r--
calendar.html
37.79
KB
-rw-r--r--
carbon.html
48.94
KB
-rw-r--r--
cd.html
27.96
KB
-rw-r--r--
cgi.html
49.92
KB
-rw-r--r--
cgihttpserver.html
13.1
KB
-rw-r--r--
cgitb.html
11.41
KB
-rw-r--r--
chunk.html
14.66
KB
-rw-r--r--
cmath.html
25.63
KB
-rw-r--r--
cmd.html
26.09
KB
-rw-r--r--
code.html
24.58
KB
-rw-r--r--
codecs.html
100.64
KB
-rw-r--r--
codeop.html
14.84
KB
-rw-r--r--
collections.html
133.96
KB
-rw-r--r--
colorpicker.html
7.52
KB
-rw-r--r--
colorsys.html
11.04
KB
-rw-r--r--
commands.html
14.36
KB
-rw-r--r--
compileall.html
16.83
KB
-rw-r--r--
compiler.html
67.75
KB
-rw-r--r--
configparser.html
62.13
KB
-rw-r--r--
constants.html
12.83
KB
-rw-r--r--
contextlib.html
19.39
KB
-rw-r--r--
cookie.html
39.07
KB
-rw-r--r--
cookielib.html
83.82
KB
-rw-r--r--
copy.html
12.19
KB
-rw-r--r--
copy_reg.html
13.76
KB
-rw-r--r--
crypt.html
10.04
KB
-rw-r--r--
crypto.html
7.59
KB
-rw-r--r--
csv.html
67.37
KB
-rw-r--r--
ctypes.html
238.78
KB
-rw-r--r--
curses.ascii.html
22.29
KB
-rw-r--r--
curses.html
146.63
KB
-rw-r--r--
curses.panel.html
14.39
KB
-rw-r--r--
custominterp.html
7.62
KB
-rw-r--r--
datatypes.html
16.84
KB
-rw-r--r--
datetime.html
226.59
KB
-rw-r--r--
dbhash.html
15.48
KB
-rw-r--r--
dbm.html
12.07
KB
-rw-r--r--
debug.html
10.15
KB
-rw-r--r--
decimal.html
194.44
KB
-rw-r--r--
development.html
14.17
KB
-rw-r--r--
difflib.html
84.83
KB
-rw-r--r--
dircache.html
11.41
KB
-rw-r--r--
dis.html
69.95
KB
-rw-r--r--
distutils.html
8.05
KB
-rw-r--r--
dl.html
16.33
KB
-rw-r--r--
doctest.html
165.54
KB
-rw-r--r--
docxmlrpcserver.html
16.43
KB
-rw-r--r--
dumbdbm.html
14.02
KB
-rw-r--r--
dummy_thread.html
9.43
KB
-rw-r--r--
dummy_threading.html
8.37
KB
-rw-r--r--
easydialogs.html
30.55
KB
-rw-r--r--
email-examples.html
45.65
KB
-rw-r--r--
email.charset.html
26.8
KB
-rw-r--r--
email.encoders.html
11.86
KB
-rw-r--r--
email.errors.html
15.77
KB
-rw-r--r--
email.generator.html
20.77
KB
-rw-r--r--
email.header.html
26.92
KB
-rw-r--r--
email.html
44.24
KB
-rw-r--r--
email.iterators.html
11.52
KB
-rw-r--r--
email.message.html
63.16
KB
-rw-r--r--
email.mime.html
27.93
KB
-rw-r--r--
email.parser.html
30.45
KB
-rw-r--r--
email.util.html
24.46
KB
-rw-r--r--
errno.html
37.99
KB
-rw-r--r--
exceptions.html
56.13
KB
-rw-r--r--
fcntl.html
22.67
KB
-rw-r--r--
filecmp.html
22.3
KB
-rw-r--r--
fileformats.html
9.14
KB
-rw-r--r--
fileinput.html
24.28
KB
-rw-r--r--
filesys.html
10.2
KB
-rw-r--r--
fl.html
49.92
KB
-rw-r--r--
fm.html
11.91
KB
-rw-r--r--
fnmatch.html
14.58
KB
-rw-r--r--
formatter.html
34.06
KB
-rw-r--r--
fpectl.html
16.01
KB
-rw-r--r--
fpformat.html
10.59
KB
-rw-r--r--
fractions.html
22.61
KB
-rw-r--r--
framework.html
33.34
KB
-rw-r--r--
frameworks.html
7.14
KB
-rw-r--r--
ftplib.html
43.99
KB
-rw-r--r--
functions.html
183.14
KB
-rw-r--r--
functools.html
27.17
KB
-rw-r--r--
future_builtins.html
13.04
KB
-rw-r--r--
gc.html
25.75
KB
-rw-r--r--
gdbm.html
15.96
KB
-rw-r--r--
gensuitemodule.html
11.51
KB
-rw-r--r--
getopt.html
23.66
KB
-rw-r--r--
getpass.html
10.65
KB
-rw-r--r--
gettext.html
78.76
KB
-rw-r--r--
gl.html
22.09
KB
-rw-r--r--
glob.html
13.26
KB
-rw-r--r--
grp.html
10.49
KB
-rw-r--r--
gzip.html
18.99
KB
-rw-r--r--
hashlib.html
18.2
KB
-rw-r--r--
heapq.html
31.61
KB
-rw-r--r--
hmac.html
10.46
KB
-rw-r--r--
hotshot.html
18.65
KB
-rw-r--r--
htmllib.html
25.32
KB
-rw-r--r--
htmlparser.html
39.11
KB
-rw-r--r--
httplib.html
62.95
KB
-rw-r--r--
i18n.html
9.52
KB
-rw-r--r--
ic.html
17.17
KB
-rw-r--r--
idle.html
20.9
KB
-rw-r--r--
imageop.html
14.76
KB
-rw-r--r--
imaplib.html
51.99
KB
-rw-r--r--
imgfile.html
11.71
KB
-rw-r--r--
imghdr.html
11.3
KB
-rw-r--r--
imp.html
34.34
KB
-rw-r--r--
importlib.html
8.26
KB
-rw-r--r--
imputil.html
31.81
KB
-rw-r--r--
index.html
72.78
KB
-rw-r--r--
inspect.html
50.71
KB
-rw-r--r--
internet.html
24.87
KB
-rw-r--r--
intro.html
8.93
KB
-rw-r--r--
io.html
98.13
KB
-rw-r--r--
ipc.html
13.41
KB
-rw-r--r--
itertools.html
115.91
KB
-rw-r--r--
jpeg.html
12.74
KB
-rw-r--r--
json.html
67.04
KB
-rw-r--r--
keyword.html
7.68
KB
-rw-r--r--
language.html
11.03
KB
-rw-r--r--
linecache.html
10.59
KB
-rw-r--r--
locale.html
55.14
KB
-rw-r--r--
logging.config.html
63.36
KB
-rw-r--r--
logging.handlers.html
69.64
KB
-rw-r--r--
logging.html
95.64
KB
-rw-r--r--
mac.html
21.79
KB
-rw-r--r--
macos.html
14.76
KB
-rw-r--r--
macosa.html
12.96
KB
-rw-r--r--
macostools.html
15.52
KB
-rw-r--r--
macpath.html
7.76
KB
-rw-r--r--
mailbox.html
156.75
KB
-rw-r--r--
mailcap.html
13.21
KB
-rw-r--r--
markup.html
18.77
KB
-rw-r--r--
marshal.html
17.98
KB
-rw-r--r--
math.html
39.24
KB
-rw-r--r--
md5.html
13.97
KB
-rw-r--r--
mhlib.html
21.54
KB
-rw-r--r--
mimetools.html
19.25
KB
-rw-r--r--
mimetypes.html
28.39
KB
-rw-r--r--
mimewriter.html
15.02
KB
-rw-r--r--
mimify.html
13.36
KB
-rw-r--r--
miniaeframe.html
12.2
KB
-rw-r--r--
misc.html
6.87
KB
-rw-r--r--
mm.html
9.03
KB
-rw-r--r--
mmap.html
28.36
KB
-rw-r--r--
modulefinder.html
15.31
KB
-rw-r--r--
modules.html
8.46
KB
-rw-r--r--
msilib.html
52.43
KB
-rw-r--r--
msvcrt.html
19.37
KB
-rw-r--r--
multifile.html
24.3
KB
-rw-r--r--
multiprocessing.html
365.71
KB
-rw-r--r--
mutex.html
11.23
KB
-rw-r--r--
netdata.html
16.98
KB
-rw-r--r--
netrc.html
12.3
KB
-rw-r--r--
new.html
12.12
KB
-rw-r--r--
nis.html
10.64
KB
-rw-r--r--
nntplib.html
41.92
KB
-rw-r--r--
numbers.html
37.75
KB
-rw-r--r--
numeric.html
13.55
KB
-rw-r--r--
operator.html
82
KB
-rw-r--r--
optparse.html
222.56
KB
-rw-r--r--
os.html
214.25
KB
-rw-r--r--
os.path.html
38.34
KB
-rw-r--r--
ossaudiodev.html
41.5
KB
-rw-r--r--
othergui.html
9.08
KB
-rw-r--r--
parser.html
39.36
KB
-rw-r--r--
pdb.html
33.96
KB
-rw-r--r--
persistence.html
14.87
KB
-rw-r--r--
pickle.html
102.27
KB
-rw-r--r--
pickletools.html
10.63
KB
-rw-r--r--
pipes.html
18.01
KB
-rw-r--r--
pkgutil.html
25.11
KB
-rw-r--r--
platform.html
28.37
KB
-rw-r--r--
plistlib.html
17.03
KB
-rw-r--r--
popen2.html
25.43
KB
-rw-r--r--
poplib.html
22.32
KB
-rw-r--r--
posix.html
14.41
KB
-rw-r--r--
posixfile.html
19.76
KB
-rw-r--r--
pprint.html
29.92
KB
-rw-r--r--
profile.html
63.56
KB
-rw-r--r--
pty.html
9.48
KB
-rw-r--r--
pwd.html
11.43
KB
-rw-r--r--
py_compile.html
11.12
KB
-rw-r--r--
pyclbr.html
14.71
KB
-rw-r--r--
pydoc.html
11.48
KB
-rw-r--r--
pyexpat.html
71.53
KB
-rw-r--r--
python.html
12.27
KB
-rw-r--r--
queue.html
24.22
KB
-rw-r--r--
quopri.html
11.9
KB
-rw-r--r--
random.html
37.83
KB
-rw-r--r--
re.html
134.74
KB
-rw-r--r--
readline.html
28.24
KB
-rw-r--r--
repr.html
20.43
KB
-rw-r--r--
resource.html
26.48
KB
-rw-r--r--
restricted.html
11.65
KB
-rw-r--r--
rexec.html
37.41
KB
-rw-r--r--
rfc822.html
42.22
KB
-rw-r--r--
rlcompleter.html
13.51
KB
-rw-r--r--
robotparser.html
12.27
KB
-rw-r--r--
runpy.html
19.34
KB
-rw-r--r--
sched.html
18.54
KB
-rw-r--r--
scrolledtext.html
9.32
KB
-rw-r--r--
select.html
39.67
KB
-rw-r--r--
sets.html
36.92
KB
-rw-r--r--
sgi.html
9.71
KB
-rw-r--r--
sgmllib.html
30.77
KB
-rw-r--r--
sha.html
12.09
KB
-rw-r--r--
shelve.html
27.02
KB
-rw-r--r--
shlex.html
32.1
KB
-rw-r--r--
shutil.html
40.22
KB
-rw-r--r--
signal.html
31.14
KB
-rw-r--r--
simplehttpserver.html
18.41
KB
-rw-r--r--
simplexmlrpcserver.html
31.39
KB
-rw-r--r--
site.html
23.64
KB
-rw-r--r--
smtpd.html
12.46
KB
-rw-r--r--
smtplib.html
42.13
KB
-rw-r--r--
sndhdr.html
10.02
KB
-rw-r--r--
socket.html
106.34
KB
-rw-r--r--
socketserver.html
59.83
KB
-rw-r--r--
someos.html
15.11
KB
-rw-r--r--
spwd.html
10.33
KB
-rw-r--r--
sqlite3.html
139.5
KB
-rw-r--r--
ssl.html
65.62
KB
-rw-r--r--
stat.html
32.31
KB
-rw-r--r--
statvfs.html
10.6
KB
-rw-r--r--
stdtypes.html
260.4
KB
-rw-r--r--
string.html
106.65
KB
-rw-r--r--
stringio.html
18.81
KB
-rw-r--r--
stringprep.html
16.13
KB
-rw-r--r--
strings.html
14.93
KB
-rw-r--r--
struct.html
40.88
KB
-rw-r--r--
subprocess.html
84.91
KB
-rw-r--r--
sun.html
6.84
KB
-rw-r--r--
sunau.html
27.1
KB
-rw-r--r--
sunaudio.html
17.79
KB
-rw-r--r--
symbol.html
7.66
KB
-rw-r--r--
symtable.html
22.94
KB
-rw-r--r--
sys.html
98.7
KB
-rw-r--r--
sysconfig.html
23.84
KB
-rw-r--r--
syslog.html
17.92
KB
-rw-r--r--
tabnanny.html
10.63
KB
-rw-r--r--
tarfile.html
78.68
KB
-rw-r--r--
telnetlib.html
25.48
KB
-rw-r--r--
tempfile.html
29.42
KB
-rw-r--r--
termios.html
16.01
KB
-rw-r--r--
test.html
52.62
KB
-rw-r--r--
textwrap.html
27.25
KB
-rw-r--r--
thread.html
20.47
KB
-rw-r--r--
threading.html
76.69
KB
-rw-r--r--
time.html
56.93
KB
-rw-r--r--
timeit.html
36.27
KB
-rw-r--r--
tix.html
46.96
KB
-rw-r--r--
tk.html
23.64
KB
-rw-r--r--
tkinter.html
67.67
KB
-rw-r--r--
token.html
19.62
KB
-rw-r--r--
tokenize.html
18.45
KB
-rw-r--r--
trace.html
25.54
KB
-rw-r--r--
traceback.html
33.44
KB
-rw-r--r--
ttk.html
101.75
KB
-rw-r--r--
tty.html
9.06
KB
-rw-r--r--
turtle.html
211.74
KB
-rw-r--r--
types.html
27.59
KB
-rw-r--r--
undoc.html
23.16
KB
-rw-r--r--
unicodedata.html
18.55
KB
-rw-r--r--
unittest.html
202.85
KB
-rw-r--r--
unix.html
10.55
KB
-rw-r--r--
urllib.html
58.68
KB
-rw-r--r--
urllib2.html
100.58
KB
-rw-r--r--
urlparse.html
40.41
KB
-rw-r--r--
user.html
11.83
KB
-rw-r--r--
userdict.html
29.73
KB
-rw-r--r--
uu.html
11.03
KB
-rw-r--r--
uuid.html
28.19
KB
-rw-r--r--
warnings.html
46.6
KB
-rw-r--r--
wave.html
22.22
KB
-rw-r--r--
weakref.html
36.52
KB
-rw-r--r--
webbrowser.html
23.07
KB
-rw-r--r--
whichdb.html
8.85
KB
-rw-r--r--
windows.html
9.33
KB
-rw-r--r--
winsound.html
18.75
KB
-rw-r--r--
wsgiref.html
81.04
KB
-rw-r--r--
xdrlib.html
29.94
KB
-rw-r--r--
xml.dom.html
89.04
KB
-rw-r--r--
xml.dom.minidom.html
40.42
KB
-rw-r--r--
xml.dom.pulldom.html
12.71
KB
-rw-r--r--
xml.etree.elementtree.html
93.22
KB
-rw-r--r--
xml.html
16.49
KB
-rw-r--r--
xml.sax.handler.html
38.63
KB
-rw-r--r--
xml.sax.html
20.22
KB
-rw-r--r--
xml.sax.reader.html
39.09
KB
-rw-r--r--
xml.sax.utils.html
14.26
KB
-rw-r--r--
xmlrpclib.html
60.79
KB
-rw-r--r--
zipfile.html
53.14
KB
-rw-r--r--
zipimport.html
20.42
KB
-rw-r--r--
zlib.html
25.46
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : pickle.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>11.1. pickle — Python object serialization — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="11. Data Persistence" href="persistence.html" /> <link rel="next" title="11.3. copy_reg — Register pickle support functions" href="copy_reg.html" /> <link rel="prev" title="11. Data Persistence" href="persistence.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="copy_reg.html" title="11.3. copy_reg — Register pickle support functions" accesskey="N">next</a> |</li> <li class="right" > <a href="persistence.html" title="11. Data Persistence" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="persistence.html" accesskey="U">11. Data Persistence</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="pickle-python-object-serialization"> <h1>11.1. <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> — Python object serialization<a class="headerlink" href="#pickle-python-object-serialization" title="Permalink to this headline">¶</a></h1> <span class="target" id="module-pickle"><span id="index-0"></span></span><p>The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” <a class="footnote-reference" href="#id11" id="id1">[1]</a> or “flattening”, however, to avoid confusion, the terms used here are “pickling” and “unpickling”.</p> <p>This documentation describes both the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module and the <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module is not intended to be secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.</p> </div> <div class="section" id="relationship-to-other-python-modules"> <h2>11.1.1. Relationship to other Python modules<a class="headerlink" href="#relationship-to-other-python-modules" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module has an optimized cousin called the <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module. As its name implies, <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> is written in C, so it can be up to 1000 times faster than <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a>. However it does not support subclassing of the <a class="reference internal" href="#pickle.Pickler" title="pickle.Pickler"><tt class="xref py py-func docutils literal"><span class="pre">Pickler()</span></tt></a> and <a class="reference internal" href="#pickle.Unpickler" title="pickle.Unpickler"><tt class="xref py py-func docutils literal"><span class="pre">Unpickler()</span></tt></a> classes, because in <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> these are functions, not classes. Most applications have no need for this functionality, and can benefit from the improved performance of <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a>. Other than that, the interfaces of the two modules are nearly identical; the common interface is described in this manual and differences are pointed out where necessary. In the following discussions, we use the term “pickle” to collectively describe the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> and <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> modules.</p> <p>The data streams the two modules produce are guaranteed to be interchangeable.</p> <p>Python has a more primitive serialization module called <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a>, but in general <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> should always be the preferred way to serialize Python objects. <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a> exists primarily to support Python’s <tt class="file docutils literal"><span class="pre">.pyc</span></tt> files.</p> <p>The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module differs from <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a> in several significant ways:</p> <ul> <li><p class="first">The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module keeps track of the objects it has already serialized, so that later references to the same object won’t be serialized again. <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a> doesn’t do this.</p> <p>This has implications both for recursive objects and object sharing. Recursive objects are objects that contain references to themselves. These are not handled by marshal, and in fact, attempting to marshal recursive objects will crash your Python interpreter. Object sharing happens when there are multiple references to the same object in different places in the object hierarchy being serialized. <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> stores such objects only once, and ensures that all other references point to the master copy. Shared objects remain shared, which can be very important for mutable objects.</p> </li> <li><p class="first"><a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a> cannot be used to serialize user-defined classes and their instances. <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> can save and restore class instances transparently, however the class definition must be importable and live in the same module as when the object was stored.</p> </li> <li><p class="first">The <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a> serialization format is not guaranteed to be portable across Python versions. Because its primary job in life is to support <tt class="file docutils literal"><span class="pre">.pyc</span></tt> files, the Python implementers reserve the right to change the serialization format in non-backwards compatible ways should the need arise. The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> serialization format is guaranteed to be backwards compatible across Python releases.</p> </li> </ul> <p>Note that serialization is a more primitive notion than persistence; although <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> reads and writes file objects, it does not handle the issue of naming persistent objects, nor the (even more complicated) issue of concurrent access to persistent objects. The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module can transform a complex object into a byte stream and it can transform the byte stream into an object with the same internal structure. Perhaps the most obvious thing to do with these byte streams is to write them onto a file, but it is also conceivable to send them across a network or store them in a database. The module <a class="reference internal" href="shelve.html#module-shelve" title="shelve: Python object persistence."><tt class="xref py py-mod docutils literal"><span class="pre">shelve</span></tt></a> provides a simple interface to pickle and unpickle objects on DBM-style database files.</p> </div> <div class="section" id="data-stream-format"> <h2>11.1.2. Data stream format<a class="headerlink" href="#data-stream-format" title="Permalink to this headline">¶</a></h2> <p id="index-1">The data format used by <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> is Python-specific. This has the advantage that there are no restrictions imposed by external standards such as XDR (which can’t represent pointer sharing); however it means that non-Python programs may not be able to reconstruct pickled Python objects.</p> <p>By default, the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> data format uses a printable ASCII representation. This is slightly more voluminous than a binary representation. The big advantage of using printable ASCII (and of some other characteristics of <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a>‘s representation) is that for debugging or recovery purposes it is possible for a human to read the pickled file with a standard text editor.</p> <p>There are currently 3 different protocols which can be used for pickling.</p> <ul class="simple"> <li>Protocol version 0 is the original ASCII protocol and is backwards compatible with earlier versions of Python.</li> <li>Protocol version 1 is the old binary format which is also compatible with earlier versions of Python.</li> <li>Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of <a class="reference internal" href="../glossary.html#term-new-style-class"><em class="xref std std-term">new-style class</em></a>es.</li> </ul> <p>Refer to <span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0307"><strong>PEP 307</strong></a> for more information.</p> <p>If a <em>protocol</em> is not specified, protocol 0 is used. If <em>protocol</em> is specified as a negative value or <a class="reference internal" href="#pickle.HIGHEST_PROTOCOL" title="pickle.HIGHEST_PROTOCOL"><tt class="xref py py-const docutils literal"><span class="pre">HIGHEST_PROTOCOL</span></tt></a>, the highest protocol version available will be used.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.3: </span>Introduced the <em>protocol</em> parameter.</p> <p>A binary format, which is slightly more efficient, can be chosen by specifying a <em>protocol</em> version >= 1.</p> </div> <div class="section" id="usage"> <h2>11.1.3. Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2> <p>To serialize an object hierarchy, you first create a pickler, then you call the pickler’s <a class="reference internal" href="#pickle.dump" title="pickle.dump"><tt class="xref py py-meth docutils literal"><span class="pre">dump()</span></tt></a> method. To de-serialize a data stream, you first create an unpickler, then you call the unpickler’s <a class="reference internal" href="#pickle.load" title="pickle.load"><tt class="xref py py-meth docutils literal"><span class="pre">load()</span></tt></a> method. The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module provides the following constant:</p> <dl class="data"> <dt id="pickle.HIGHEST_PROTOCOL"> <tt class="descclassname">pickle.</tt><tt class="descname">HIGHEST_PROTOCOL</tt><a class="headerlink" href="#pickle.HIGHEST_PROTOCOL" title="Permalink to this definition">¶</a></dt> <dd><p>The highest protocol version available. This value can be passed as a <em>protocol</em> value.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>Be sure to always open pickle files created with protocols >= 1 in binary mode. For the old ASCII-based pickle protocol 0 you can use either text mode or binary mode as long as you stay consistent.</p> <p class="last">A pickle file written with protocol 0 in binary mode will contain lone linefeeds as line terminators and therefore will look “funny” when viewed in Notepad or other editors which do not support this format.</p> </div> <p>The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module provides the following functions to make the pickling process more convenient:</p> <dl class="function"> <dt id="pickle.dump"> <tt class="descclassname">pickle.</tt><tt class="descname">dump</tt><big>(</big><em>obj</em>, <em>file</em><span class="optional">[</span>, <em>protocol</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#pickle.dump" title="Permalink to this definition">¶</a></dt> <dd><p>Write a pickled representation of <em>obj</em> to the open file object <em>file</em>. This is equivalent to <tt class="docutils literal"><span class="pre">Pickler(file,</span> <span class="pre">protocol).dump(obj)</span></tt>.</p> <p>If the <em>protocol</em> parameter is omitted, protocol 0 is used. If <em>protocol</em> is specified as a negative value or <a class="reference internal" href="#pickle.HIGHEST_PROTOCOL" title="pickle.HIGHEST_PROTOCOL"><tt class="xref py py-const docutils literal"><span class="pre">HIGHEST_PROTOCOL</span></tt></a>, the highest protocol version will be used.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.3: </span>Introduced the <em>protocol</em> parameter.</p> <p><em>file</em> must have a <tt class="xref py py-meth docutils literal"><span class="pre">write()</span></tt> method that accepts a single string argument. It can thus be a file object opened for writing, a <a class="reference internal" href="stringio.html#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> object, or any other custom object that meets this interface.</p> </dd></dl> <dl class="function"> <dt id="pickle.load"> <tt class="descclassname">pickle.</tt><tt class="descname">load</tt><big>(</big><em>file</em><big>)</big><a class="headerlink" href="#pickle.load" title="Permalink to this definition">¶</a></dt> <dd><p>Read a string from the open file object <em>file</em> and interpret it as a pickle data stream, reconstructing and returning the original object hierarchy. This is equivalent to <tt class="docutils literal"><span class="pre">Unpickler(file).load()</span></tt>.</p> <p><em>file</em> must have two methods, a <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt> method that takes an integer argument, and a <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> method that requires no arguments. Both methods should return a string. Thus <em>file</em> can be a file object opened for reading, a <a class="reference internal" href="stringio.html#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> object, or any other custom object that meets this interface.</p> <p>This function automatically determines whether the data stream was written in binary mode or not.</p> </dd></dl> <dl class="function"> <dt id="pickle.dumps"> <tt class="descclassname">pickle.</tt><tt class="descname">dumps</tt><big>(</big><em>obj</em><span class="optional">[</span>, <em>protocol</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#pickle.dumps" title="Permalink to this definition">¶</a></dt> <dd><p>Return the pickled representation of the object as a string, instead of writing it to a file.</p> <p>If the <em>protocol</em> parameter is omitted, protocol 0 is used. If <em>protocol</em> is specified as a negative value or <a class="reference internal" href="#pickle.HIGHEST_PROTOCOL" title="pickle.HIGHEST_PROTOCOL"><tt class="xref py py-const docutils literal"><span class="pre">HIGHEST_PROTOCOL</span></tt></a>, the highest protocol version will be used.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.3: </span>The <em>protocol</em> parameter was added.</p> </dd></dl> <dl class="function"> <dt id="pickle.loads"> <tt class="descclassname">pickle.</tt><tt class="descname">loads</tt><big>(</big><em>string</em><big>)</big><a class="headerlink" href="#pickle.loads" title="Permalink to this definition">¶</a></dt> <dd><p>Read a pickled object hierarchy from a string. Characters in the string past the pickled object’s representation are ignored.</p> </dd></dl> <p>The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module also defines three exceptions:</p> <dl class="exception"> <dt id="pickle.PickleError"> <em class="property">exception </em><tt class="descclassname">pickle.</tt><tt class="descname">PickleError</tt><a class="headerlink" href="#pickle.PickleError" title="Permalink to this definition">¶</a></dt> <dd><p>A common base class for the other exceptions defined below. This inherits from <a class="reference internal" href="exceptions.html#exceptions.Exception" title="exceptions.Exception"><tt class="xref py py-exc docutils literal"><span class="pre">Exception</span></tt></a>.</p> </dd></dl> <dl class="exception"> <dt id="pickle.PicklingError"> <em class="property">exception </em><tt class="descclassname">pickle.</tt><tt class="descname">PicklingError</tt><a class="headerlink" href="#pickle.PicklingError" title="Permalink to this definition">¶</a></dt> <dd><p>This exception is raised when an unpicklable object is passed to the <a class="reference internal" href="#pickle.dump" title="pickle.dump"><tt class="xref py py-meth docutils literal"><span class="pre">dump()</span></tt></a> method.</p> </dd></dl> <dl class="exception"> <dt id="pickle.UnpicklingError"> <em class="property">exception </em><tt class="descclassname">pickle.</tt><tt class="descname">UnpicklingError</tt><a class="headerlink" href="#pickle.UnpicklingError" title="Permalink to this definition">¶</a></dt> <dd><p>This exception is raised when there is a problem unpickling an object. Note that other exceptions may also be raised during unpickling, including (but not necessarily limited to) <a class="reference internal" href="exceptions.html#exceptions.AttributeError" title="exceptions.AttributeError"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a>, <a class="reference internal" href="exceptions.html#exceptions.EOFError" title="exceptions.EOFError"><tt class="xref py py-exc docutils literal"><span class="pre">EOFError</span></tt></a>, <a class="reference internal" href="exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a>, and <a class="reference internal" href="exceptions.html#exceptions.IndexError" title="exceptions.IndexError"><tt class="xref py py-exc docutils literal"><span class="pre">IndexError</span></tt></a>.</p> </dd></dl> <p>The <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module also exports two callables <a class="footnote-reference" href="#id12" id="id2">[2]</a>, <a class="reference internal" href="#pickle.Pickler" title="pickle.Pickler"><tt class="xref py py-class docutils literal"><span class="pre">Pickler</span></tt></a> and <a class="reference internal" href="#pickle.Unpickler" title="pickle.Unpickler"><tt class="xref py py-class docutils literal"><span class="pre">Unpickler</span></tt></a>:</p> <dl class="class"> <dt id="pickle.Pickler"> <em class="property">class </em><tt class="descclassname">pickle.</tt><tt class="descname">Pickler</tt><big>(</big><em>file</em><span class="optional">[</span>, <em>protocol</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#pickle.Pickler" title="Permalink to this definition">¶</a></dt> <dd><p>This takes a file-like object to which it will write a pickle data stream.</p> <p>If the <em>protocol</em> parameter is omitted, protocol 0 is used. If <em>protocol</em> is specified as a negative value or <a class="reference internal" href="#pickle.HIGHEST_PROTOCOL" title="pickle.HIGHEST_PROTOCOL"><tt class="xref py py-const docutils literal"><span class="pre">HIGHEST_PROTOCOL</span></tt></a>, the highest protocol version will be used.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.3: </span>Introduced the <em>protocol</em> parameter.</p> <p><em>file</em> must have a <tt class="xref py py-meth docutils literal"><span class="pre">write()</span></tt> method that accepts a single string argument. It can thus be an open file object, a <a class="reference internal" href="stringio.html#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> object, or any other custom object that meets this interface.</p> <p><a class="reference internal" href="#pickle.Pickler" title="pickle.Pickler"><tt class="xref py py-class docutils literal"><span class="pre">Pickler</span></tt></a> objects define one (or two) public methods:</p> <dl class="method"> <dt id="pickle.Pickler.dump"> <tt class="descname">dump</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#pickle.Pickler.dump" title="Permalink to this definition">¶</a></dt> <dd><p>Write a pickled representation of <em>obj</em> to the open file object given in the constructor. Either the binary or ASCII format will be used, depending on the value of the <em>protocol</em> argument passed to the constructor.</p> </dd></dl> <dl class="method"> <dt id="pickle.Pickler.clear_memo"> <tt class="descname">clear_memo</tt><big>(</big><big>)</big><a class="headerlink" href="#pickle.Pickler.clear_memo" title="Permalink to this definition">¶</a></dt> <dd><p>Clears the pickler’s “memo”. The memo is the data structure that remembers which objects the pickler has already seen, so that shared or recursive objects pickled by reference and not by value. This method is useful when re-using picklers.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>Prior to Python 2.3, <a class="reference internal" href="#pickle.Pickler.clear_memo" title="pickle.Pickler.clear_memo"><tt class="xref py py-meth docutils literal"><span class="pre">clear_memo()</span></tt></a> was only available on the picklers created by <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a>. In the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module, picklers have an instance variable called <tt class="xref py py-attr docutils literal"><span class="pre">memo</span></tt> which is a Python dictionary. So to clear the memo for a <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module pickler, you could do the following:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">mypickler</span><span class="o">.</span><span class="n">memo</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span> </pre></div> </div> <p class="last">Code that does not need to support older versions of Python should simply use <a class="reference internal" href="#pickle.Pickler.clear_memo" title="pickle.Pickler.clear_memo"><tt class="xref py py-meth docutils literal"><span class="pre">clear_memo()</span></tt></a>.</p> </div> </dd></dl> </dd></dl> <p>It is possible to make multiple calls to the <a class="reference internal" href="#pickle.dump" title="pickle.dump"><tt class="xref py py-meth docutils literal"><span class="pre">dump()</span></tt></a> method of the same <a class="reference internal" href="#pickle.Pickler" title="pickle.Pickler"><tt class="xref py py-class docutils literal"><span class="pre">Pickler</span></tt></a> instance. These must then be matched to the same number of calls to the <a class="reference internal" href="#pickle.load" title="pickle.load"><tt class="xref py py-meth docutils literal"><span class="pre">load()</span></tt></a> method of the corresponding <a class="reference internal" href="#pickle.Unpickler" title="pickle.Unpickler"><tt class="xref py py-class docutils literal"><span class="pre">Unpickler</span></tt></a> instance. If the same object is pickled by multiple <a class="reference internal" href="#pickle.dump" title="pickle.dump"><tt class="xref py py-meth docutils literal"><span class="pre">dump()</span></tt></a> calls, the <a class="reference internal" href="#pickle.load" title="pickle.load"><tt class="xref py py-meth docutils literal"><span class="pre">load()</span></tt></a> will all yield references to the same object. <a class="footnote-reference" href="#id13" id="id3">[3]</a></p> <p><a class="reference internal" href="#pickle.Unpickler" title="pickle.Unpickler"><tt class="xref py py-class docutils literal"><span class="pre">Unpickler</span></tt></a> objects are defined as:</p> <dl class="class"> <dt id="pickle.Unpickler"> <em class="property">class </em><tt class="descclassname">pickle.</tt><tt class="descname">Unpickler</tt><big>(</big><em>file</em><big>)</big><a class="headerlink" href="#pickle.Unpickler" title="Permalink to this definition">¶</a></dt> <dd><p>This takes a file-like object from which it will read a pickle data stream. This class automatically determines whether the data stream was written in binary mode or not, so it does not need a flag as in the <a class="reference internal" href="#pickle.Pickler" title="pickle.Pickler"><tt class="xref py py-class docutils literal"><span class="pre">Pickler</span></tt></a> factory.</p> <p><em>file</em> must have two methods, a <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt> method that takes an integer argument, and a <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> method that requires no arguments. Both methods should return a string. Thus <em>file</em> can be a file object opened for reading, a <a class="reference internal" href="stringio.html#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> object, or any other custom object that meets this interface.</p> <p><a class="reference internal" href="#pickle.Unpickler" title="pickle.Unpickler"><tt class="xref py py-class docutils literal"><span class="pre">Unpickler</span></tt></a> objects have one (or two) public methods:</p> <dl class="method"> <dt id="pickle.Unpickler.load"> <tt class="descname">load</tt><big>(</big><big>)</big><a class="headerlink" href="#pickle.Unpickler.load" title="Permalink to this definition">¶</a></dt> <dd><p>Read a pickled object representation from the open file object given in the constructor, and return the reconstituted object hierarchy specified therein.</p> <p>This method automatically determines whether the data stream was written in binary mode or not.</p> </dd></dl> <dl class="method"> <dt id="pickle.Unpickler.noload"> <tt class="descname">noload</tt><big>(</big><big>)</big><a class="headerlink" href="#pickle.Unpickler.noload" title="Permalink to this definition">¶</a></dt> <dd><p>This is just like <a class="reference internal" href="#pickle.load" title="pickle.load"><tt class="xref py py-meth docutils literal"><span class="pre">load()</span></tt></a> except that it doesn’t actually create any objects. This is useful primarily for finding what’s called “persistent ids” that may be referenced in a pickle data stream. See section <a class="reference internal" href="#pickle-protocol"><em>The pickle protocol</em></a> below for more details.</p> <p><strong>Note:</strong> the <a class="reference internal" href="#pickle.Unpickler.noload" title="pickle.Unpickler.noload"><tt class="xref py py-meth docutils literal"><span class="pre">noload()</span></tt></a> method is currently only available on <a class="reference internal" href="#pickle.Unpickler" title="pickle.Unpickler"><tt class="xref py py-class docutils literal"><span class="pre">Unpickler</span></tt></a> objects created with the <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module. <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module <a class="reference internal" href="#pickle.Unpickler" title="pickle.Unpickler"><tt class="xref py py-class docutils literal"><span class="pre">Unpickler</span></tt></a>s do not have the <a class="reference internal" href="#pickle.Unpickler.noload" title="pickle.Unpickler.noload"><tt class="xref py py-meth docutils literal"><span class="pre">noload()</span></tt></a> method.</p> </dd></dl> </dd></dl> </div> <div class="section" id="what-can-be-pickled-and-unpickled"> <h2>11.1.4. What can be pickled and unpickled?<a class="headerlink" href="#what-can-be-pickled-and-unpickled" title="Permalink to this headline">¶</a></h2> <p>The following types can be pickled:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">None</span></tt>, <tt class="docutils literal"><span class="pre">True</span></tt>, and <tt class="docutils literal"><span class="pre">False</span></tt></li> <li>integers, long integers, floating point numbers, complex numbers</li> <li>normal and Unicode strings</li> <li>tuples, lists, sets, and dictionaries containing only picklable objects</li> <li>functions defined at the top level of a module</li> <li>built-in functions defined at the top level of a module</li> <li>classes that are defined at the top level of a module</li> <li>instances of such classes whose <tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt> or the result of calling <a class="reference internal" href="#object.__getstate__" title="object.__getstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__getstate__()</span></tt></a> is picklable (see section <a class="reference internal" href="#pickle-protocol"><em>The pickle protocol</em></a> for details).</li> </ul> <p>Attempts to pickle unpicklable objects will raise the <a class="reference internal" href="#pickle.PicklingError" title="pickle.PicklingError"><tt class="xref py py-exc docutils literal"><span class="pre">PicklingError</span></tt></a> exception; when this happens, an unspecified number of bytes may have already been written to the underlying file. Trying to pickle a highly recursive data structure may exceed the maximum recursion depth, a <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> will be raised in this case. You can carefully raise this limit with <a class="reference internal" href="sys.html#sys.setrecursionlimit" title="sys.setrecursionlimit"><tt class="xref py py-func docutils literal"><span class="pre">sys.setrecursionlimit()</span></tt></a>.</p> <p>Note that functions (built-in and user-defined) are pickled by “fully qualified” name reference, not by value. This means that only the function name is pickled, along with the name of the module the function is defined in. Neither the function’s code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised. <a class="footnote-reference" href="#id14" id="id4">[4]</a></p> <p>Similarly, classes are pickled by named reference, so the same restrictions in the unpickling environment apply. Note that none of the class’s code or data is pickled, so in the following example the class attribute <tt class="docutils literal"><span class="pre">attr</span></tt> is not restored in the unpickling environment:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span> <span class="n">attr</span> <span class="o">=</span> <span class="s">'a class attr'</span> <span class="n">picklestring</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">Foo</span><span class="p">)</span> </pre></div> </div> <p>These restrictions are why picklable functions and classes must be defined in the top level of a module.</p> <p>Similarly, when class instances are pickled, their class’s code and data are not pickled along with them. Only the instance data are pickled. This is done on purpose, so you can fix bugs in a class or add methods to the class and still load objects that were created with an earlier version of the class. If you plan to have long-lived objects that will see many versions of a class, it may be worthwhile to put a version number in the objects so that suitable conversions can be made by the class’s <a class="reference internal" href="#object.__setstate__" title="object.__setstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></tt></a> method.</p> </div> <div class="section" id="the-pickle-protocol"> <span id="pickle-protocol"></span><h2>11.1.5. The pickle protocol<a class="headerlink" href="#the-pickle-protocol" title="Permalink to this headline">¶</a></h2> <p>This section describes the “pickling protocol” that defines the interface between the pickler/unpickler and the objects that are being serialized. This protocol provides a standard way for you to define, customize, and control how your objects are serialized and de-serialized. The description in this section doesn’t cover specific customizations that you can employ to make the unpickling environment slightly safer from untrusted pickle data streams; see section <a class="reference internal" href="#pickle-sub"><em>Subclassing Unpicklers</em></a> for more details.</p> <div class="section" id="pickling-and-unpickling-normal-class-instances"> <span id="pickle-inst"></span><h3>11.1.5.1. Pickling and unpickling normal class instances<a class="headerlink" href="#pickling-and-unpickling-normal-class-instances" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="object.__getinitargs__"> <tt class="descclassname">object.</tt><tt class="descname">__getinitargs__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__getinitargs__" title="Permalink to this definition">¶</a></dt> <dd><p>When a pickled class instance is unpickled, its <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> method is normally <em>not</em> invoked. If it is desirable that the <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> method be called on unpickling, an old-style class can define a method <a class="reference internal" href="#object.__getinitargs__" title="object.__getinitargs__"><tt class="xref py py-meth docutils literal"><span class="pre">__getinitargs__()</span></tt></a>, which should return a <em>tuple</em> containing the arguments to be passed to the class constructor (<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> for example). The <a class="reference internal" href="#object.__getinitargs__" title="object.__getinitargs__"><tt class="xref py py-meth docutils literal"><span class="pre">__getinitargs__()</span></tt></a> method is called at pickle time; the tuple it returns is incorporated in the pickle for the instance.</p> </dd></dl> <dl class="method"> <dt id="object.__getnewargs__"> <tt class="descclassname">object.</tt><tt class="descname">__getnewargs__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__getnewargs__" title="Permalink to this definition">¶</a></dt> <dd><p>New-style types can provide a <a class="reference internal" href="#object.__getnewargs__" title="object.__getnewargs__"><tt class="xref py py-meth docutils literal"><span class="pre">__getnewargs__()</span></tt></a> method that is used for protocol 2. Implementing this method is needed if the type establishes some internal invariants when the instance is created, or if the memory allocation is affected by the values passed to the <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> method for the type (as it is for tuples and strings). Instances of a <a class="reference internal" href="../glossary.html#term-new-style-class"><em class="xref std std-term">new-style class</em></a> <tt class="docutils literal"><span class="pre">C</span></tt> are created using</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">obj</span> <span class="o">=</span> <span class="n">C</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">C</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span> </pre></div> </div> <p>where <em>args</em> is the result of calling <a class="reference internal" href="#object.__getnewargs__" title="object.__getnewargs__"><tt class="xref py py-meth docutils literal"><span class="pre">__getnewargs__()</span></tt></a> on the original object; if there is no <a class="reference internal" href="#object.__getnewargs__" title="object.__getnewargs__"><tt class="xref py py-meth docutils literal"><span class="pre">__getnewargs__()</span></tt></a>, an empty tuple is assumed.</p> </dd></dl> <dl class="method"> <dt id="object.__getstate__"> <tt class="descclassname">object.</tt><tt class="descname">__getstate__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__getstate__" title="Permalink to this definition">¶</a></dt> <dd><p>Classes can further influence how their instances are pickled; if the class defines the method <a class="reference internal" href="#object.__getstate__" title="object.__getstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__getstate__()</span></tt></a>, it is called and the return state is pickled as the contents for the instance, instead of the contents of the instance’s dictionary. If there is no <a class="reference internal" href="#object.__getstate__" title="object.__getstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__getstate__()</span></tt></a> method, the instance’s <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt></a> is pickled.</p> </dd></dl> <dl class="method"> <dt id="object.__setstate__"> <tt class="descclassname">object.</tt><tt class="descname">__setstate__</tt><big>(</big><em>state</em><big>)</big><a class="headerlink" href="#object.__setstate__" title="Permalink to this definition">¶</a></dt> <dd><p>Upon unpickling, if the class also defines the method <a class="reference internal" href="#object.__setstate__" title="object.__setstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></tt></a>, it is called with the unpickled state. <a class="footnote-reference" href="#id15" id="id5">[5]</a> If there is no <a class="reference internal" href="#object.__setstate__" title="object.__setstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></tt></a> method, the pickled state must be a dictionary and its items are assigned to the new instance’s dictionary. If a class defines both <a class="reference internal" href="#object.__getstate__" title="object.__getstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__getstate__()</span></tt></a> and <a class="reference internal" href="#object.__setstate__" title="object.__setstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></tt></a>, the state object needn’t be a dictionary and these methods can do what they want. <a class="footnote-reference" href="#id16" id="id6">[6]</a></p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">For <a class="reference internal" href="../glossary.html#term-new-style-class"><em class="xref std std-term">new-style class</em></a>es, if <a class="reference internal" href="#object.__getstate__" title="object.__getstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__getstate__()</span></tt></a> returns a false value, the <a class="reference internal" href="#object.__setstate__" title="object.__setstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></tt></a> method will not be called.</p> </div> </dd></dl> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">At unpickling time, some methods like <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattribute__()</span></tt></a>, or <a class="reference internal" href="../reference/datamodel.html#object.__setattr__" title="object.__setattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__setattr__()</span></tt></a> may be called upon the instance. In case those methods rely on some internal invariant being true, the type should implement either <a class="reference internal" href="#object.__getinitargs__" title="object.__getinitargs__"><tt class="xref py py-meth docutils literal"><span class="pre">__getinitargs__()</span></tt></a> or <a class="reference internal" href="#object.__getnewargs__" title="object.__getnewargs__"><tt class="xref py py-meth docutils literal"><span class="pre">__getnewargs__()</span></tt></a> to establish such an invariant; otherwise, neither <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> nor <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> will be called.</p> </div> </div> <div class="section" id="pickling-and-unpickling-extension-types"> <h3>11.1.5.2. Pickling and unpickling extension types<a class="headerlink" href="#pickling-and-unpickling-extension-types" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="object.__reduce__"> <tt class="descclassname">object.</tt><tt class="descname">__reduce__</tt><big>(</big><big>)</big><a class="headerlink" href="#object.__reduce__" title="Permalink to this definition">¶</a></dt> <dd><p>When the <tt class="xref py py-class docutils literal"><span class="pre">Pickler</span></tt> encounters an object of a type it knows nothing about — such as an extension type — it looks in two places for a hint of how to pickle it. One alternative is for the object to implement a <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> method. If provided, at pickling time <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> will be called with no arguments, and it must return either a string or a tuple.</p> <p>If a string is returned, it names a global variable whose contents are pickled as normal. The string returned by <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> should be the object’s local name relative to its module; the pickle module searches the module namespace to determine the object’s module.</p> <p>When a tuple is returned, it must be between two and five elements long. Optional elements can either be omitted, or <tt class="docutils literal"><span class="pre">None</span></tt> can be provided as their value. The contents of this tuple are pickled as normal and used to reconstruct the object at unpickling time. The semantics of each element are:</p> <ul> <li><p class="first">A callable object that will be called to create the initial version of the object. The next element of the tuple will provide arguments for this callable, and later elements provide additional state information that will subsequently be used to fully reconstruct the pickled data.</p> <p>In the unpickling environment this object must be either a class, a callable registered as a “safe constructor” (see below), or it must have an attribute <tt class="xref py py-attr docutils literal"><span class="pre">__safe_for_unpickling__</span></tt> with a true value. Otherwise, an <tt class="xref py py-exc docutils literal"><span class="pre">UnpicklingError</span></tt> will be raised in the unpickling environment. Note that as usual, the callable itself is pickled by name.</p> </li> <li><p class="first">A tuple of arguments for the callable object.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.5: </span>Formerly, this argument could also be <tt class="docutils literal"><span class="pre">None</span></tt>.</p> </li> <li><p class="first">Optionally, the object’s state, which will be passed to the object’s <a class="reference internal" href="#object.__setstate__" title="object.__setstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></tt></a> method as described in section <a class="reference internal" href="#pickle-inst"><em>Pickling and unpickling normal class instances</em></a>. If the object has no <a class="reference internal" href="#object.__setstate__" title="object.__setstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></tt></a> method, then, as above, the value must be a dictionary and it will be added to the object’s <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt></a>.</p> </li> <li><p class="first">Optionally, an iterator (and not a sequence) yielding successive list items. These list items will be pickled, and appended to the object using either <tt class="docutils literal"><span class="pre">obj.append(item)</span></tt> or <tt class="docutils literal"><span class="pre">obj.extend(list_of_items)</span></tt>. This is primarily used for list subclasses, but may be used by other classes as long as they have <tt class="xref py py-meth docutils literal"><span class="pre">append()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">extend()</span></tt> methods with the appropriate signature. (Whether <tt class="xref py py-meth docutils literal"><span class="pre">append()</span></tt> or <tt class="xref py py-meth docutils literal"><span class="pre">extend()</span></tt> is used depends on which pickle protocol version is used as well as the number of items to append, so both must be supported.)</p> </li> <li><p class="first">Optionally, an iterator (not a sequence) yielding successive dictionary items, which should be tuples of the form <tt class="docutils literal"><span class="pre">(key,</span> <span class="pre">value)</span></tt>. These items will be pickled and stored to the object using <tt class="docutils literal"><span class="pre">obj[key]</span> <span class="pre">=</span> <span class="pre">value</span></tt>. This is primarily used for dictionary subclasses, but may be used by other classes as long as they implement <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></tt></a>.</p> </li> </ul> </dd></dl> <dl class="method"> <dt id="object.__reduce_ex__"> <tt class="descclassname">object.</tt><tt class="descname">__reduce_ex__</tt><big>(</big><em>protocol</em><big>)</big><a class="headerlink" href="#object.__reduce_ex__" title="Permalink to this definition">¶</a></dt> <dd><p>It is sometimes useful to know the protocol version when implementing <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a>. This can be done by implementing a method named <a class="reference internal" href="#object.__reduce_ex__" title="object.__reduce_ex__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce_ex__()</span></tt></a> instead of <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a>. <a class="reference internal" href="#object.__reduce_ex__" title="object.__reduce_ex__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce_ex__()</span></tt></a>, when it exists, is called in preference over <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> (you may still provide <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> for backwards compatibility). The <a class="reference internal" href="#object.__reduce_ex__" title="object.__reduce_ex__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce_ex__()</span></tt></a> method will be called with a single integer argument, the protocol version.</p> <p>The <a class="reference internal" href="functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a> class implements both <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> and <a class="reference internal" href="#object.__reduce_ex__" title="object.__reduce_ex__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce_ex__()</span></tt></a>; however, if a subclass overrides <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> but not <a class="reference internal" href="#object.__reduce_ex__" title="object.__reduce_ex__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce_ex__()</span></tt></a>, the <a class="reference internal" href="#object.__reduce_ex__" title="object.__reduce_ex__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce_ex__()</span></tt></a> implementation detects this and calls <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a>.</p> </dd></dl> <p>An alternative to implementing a <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> method on the object to be pickled, is to register the callable with the <a class="reference internal" href="copy_reg.html#module-copy_reg" title="copy_reg: Register pickle support functions."><tt class="xref py py-mod docutils literal"><span class="pre">copy_reg</span></tt></a> module. This module provides a way for programs to register “reduction functions” and constructors for user-defined types. Reduction functions have the same semantics and interface as the <a class="reference internal" href="#object.__reduce__" title="object.__reduce__"><tt class="xref py py-meth docutils literal"><span class="pre">__reduce__()</span></tt></a> method described above, except that they are called with a single argument, the object to be pickled.</p> <p>The registered constructor is deemed a “safe constructor” for purposes of unpickling as described above.</p> </div> <div class="section" id="pickling-and-unpickling-external-objects"> <h3>11.1.5.3. Pickling and unpickling external objects<a class="headerlink" href="#pickling-and-unpickling-external-objects" title="Permalink to this headline">¶</a></h3> <p id="index-3">For the benefit of object persistence, the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module supports the notion of a reference to an object outside the pickled data stream. Such objects are referenced by a “persistent id”, which is just an arbitrary string of printable ASCII characters. The resolution of such names is not defined by the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module; it will delegate this resolution to user defined functions on the pickler and unpickler. <a class="footnote-reference" href="#id17" id="id7">[7]</a></p> <p>To define external persistent id resolution, you need to set the <tt class="xref py py-attr docutils literal"><span class="pre">persistent_id</span></tt> attribute of the pickler object and the <tt class="xref py py-attr docutils literal"><span class="pre">persistent_load</span></tt> attribute of the unpickler object.</p> <p>To pickle objects that have an external persistent id, the pickler must have a custom <tt class="xref py py-func docutils literal"><span class="pre">persistent_id()</span></tt> method that takes an object as an argument and returns either <tt class="docutils literal"><span class="pre">None</span></tt> or the persistent id for that object. When <tt class="docutils literal"><span class="pre">None</span></tt> is returned, the pickler simply pickles the object as normal. When a persistent id string is returned, the pickler will pickle that string, along with a marker so that the unpickler will recognize the string as a persistent id.</p> <p>To unpickle external objects, the unpickler must have a custom <tt class="xref py py-func docutils literal"><span class="pre">persistent_load()</span></tt> function that takes a persistent id string and returns the referenced object.</p> <p>Here’s a silly example that <em>might</em> shed more light:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pickle</span> <span class="kn">from</span> <span class="nn">cStringIO</span> <span class="kn">import</span> <span class="n">StringIO</span> <span class="n">src</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span> <span class="n">p</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">Pickler</span><span class="p">(</span><span class="n">src</span><span class="p">)</span> <span class="k">def</span> <span class="nf">persistent_id</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s">'x'</span><span class="p">):</span> <span class="k">return</span> <span class="s">'the value </span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="n">obj</span><span class="o">.</span><span class="n">x</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="bp">None</span> <span class="n">p</span><span class="o">.</span><span class="n">persistent_id</span> <span class="o">=</span> <span class="n">persistent_id</span> <span class="k">class</span> <span class="nc">Integer</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="s">'My name is integer </span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="n">i</span> <span class="o">=</span> <span class="n">Integer</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="k">print</span> <span class="n">i</span> <span class="n">p</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="n">datastream</span> <span class="o">=</span> <span class="n">src</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span> <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">datastream</span><span class="p">)</span> <span class="n">dst</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="n">datastream</span><span class="p">)</span> <span class="n">up</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">Unpickler</span><span class="p">(</span><span class="n">dst</span><span class="p">)</span> <span class="k">class</span> <span class="nc">FancyInteger</span><span class="p">(</span><span class="n">Integer</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="s">'I am the integer </span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="k">def</span> <span class="nf">persistent_load</span><span class="p">(</span><span class="n">persid</span><span class="p">):</span> <span class="k">if</span> <span class="n">persid</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">'the value '</span><span class="p">):</span> <span class="n">value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">persid</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">2</span><span class="p">])</span> <span class="k">return</span> <span class="n">FancyInteger</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="k">raise</span> <span class="n">pickle</span><span class="o">.</span><span class="n">UnpicklingError</span><span class="p">,</span> <span class="s">'Invalid persistent id'</span> <span class="n">up</span><span class="o">.</span><span class="n">persistent_load</span> <span class="o">=</span> <span class="n">persistent_load</span> <span class="n">j</span> <span class="o">=</span> <span class="n">up</span><span class="o">.</span><span class="n">load</span><span class="p">()</span> <span class="k">print</span> <span class="n">j</span> </pre></div> </div> <p>In the <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module, the unpickler’s <tt class="xref py py-attr docutils literal"><span class="pre">persistent_load</span></tt> attribute can also be set to a Python list, in which case, when the unpickler reaches a persistent id, the persistent id string will simply be appended to this list. This functionality exists so that a pickle data stream can be “sniffed” for object references without actually instantiating all the objects in a pickle. <a class="footnote-reference" href="#id18" id="id8">[8]</a> Setting <tt class="xref py py-attr docutils literal"><span class="pre">persistent_load</span></tt> to a list is usually used in conjunction with the <tt class="xref py py-meth docutils literal"><span class="pre">noload()</span></tt> method on the Unpickler.</p> </div> </div> <div class="section" id="subclassing-unpicklers"> <span id="pickle-sub"></span><h2>11.1.6. Subclassing Unpicklers<a class="headerlink" href="#subclassing-unpicklers" title="Permalink to this headline">¶</a></h2> <p id="index-4">By default, unpickling will import any class that it finds in the pickle data. You can control exactly what gets unpickled and what gets called by customizing your unpickler. Unfortunately, exactly how you do this is different depending on whether you’re using <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> or <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a>. <a class="footnote-reference" href="#id19" id="id9">[9]</a></p> <p>In the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module, you need to derive a subclass from <tt class="xref py py-class docutils literal"><span class="pre">Unpickler</span></tt>, overriding the <tt class="xref py py-meth docutils literal"><span class="pre">load_global()</span></tt> method. <tt class="xref py py-meth docutils literal"><span class="pre">load_global()</span></tt> should read two lines from the pickle data stream where the first line will the name of the module containing the class and the second line will be the name of the instance’s class. It then looks up the class, possibly importing the module and digging out the attribute, then it appends what it finds to the unpickler’s stack. Later on, this class will be assigned to the <tt class="xref py py-attr docutils literal"><span class="pre">__class__</span></tt> attribute of an empty class, as a way of magically creating an instance without calling its class’s <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a>. Your job (should you choose to accept it), would be to have <tt class="xref py py-meth docutils literal"><span class="pre">load_global()</span></tt> push onto the unpickler’s stack, a known safe version of any class you deem safe to unpickle. It is up to you to produce such a class. Or you could raise an error if you want to disallow all unpickling of instances. If this sounds like a hack, you’re right. Refer to the source code to make this work.</p> <p>Things are a little cleaner with <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a>, but not by much. To control what gets unpickled, you can set the unpickler’s <tt class="xref py py-attr docutils literal"><span class="pre">find_global</span></tt> attribute to a function or <tt class="docutils literal"><span class="pre">None</span></tt>. If it is <tt class="docutils literal"><span class="pre">None</span></tt> then any attempts to unpickle instances will raise an <tt class="xref py py-exc docutils literal"><span class="pre">UnpicklingError</span></tt>. If it is a function, then it should accept a module name and a class name, and return the corresponding class object. It is responsible for looking up the class and performing any necessary imports, and it may raise an error to prevent instances of the class from being unpickled.</p> <p>The moral of the story is that you should be really careful about the source of the strings your application unpickles.</p> </div> <div class="section" id="example"> <span id="pickle-example"></span><h2>11.1.7. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2> <p>For the simplest code, use the <tt class="xref py py-func docutils literal"><span class="pre">dump()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">load()</span></tt> functions. Note that a self-referencing list is pickled and restored correctly.</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pickle</span> <span class="n">data1</span> <span class="o">=</span> <span class="p">{</span><span class="s">'a'</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="o">+</span><span class="mi">6</span><span class="n">j</span><span class="p">],</span> <span class="s">'b'</span><span class="p">:</span> <span class="p">(</span><span class="s">'string'</span><span class="p">,</span> <span class="s">u'Unicode string'</span><span class="p">),</span> <span class="s">'c'</span><span class="p">:</span> <span class="bp">None</span><span class="p">}</span> <span class="n">selfref_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span> <span class="n">selfref_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">selfref_list</span><span class="p">)</span> <span class="n">output</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">'data.pkl'</span><span class="p">,</span> <span class="s">'wb'</span><span class="p">)</span> <span class="c"># Pickle dictionary using protocol 0.</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">data1</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span> <span class="c"># Pickle the list using the highest protocol available.</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">selfref_list</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> </pre></div> </div> <p>The following example reads the resulting pickled data. When reading a pickle-containing file, you should open the file in binary mode because you can’t be sure if the ASCII or binary format was used.</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pprint</span><span class="o">,</span> <span class="nn">pickle</span> <span class="n">pkl_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">'data.pkl'</span><span class="p">,</span> <span class="s">'rb'</span><span class="p">)</span> <span class="n">data1</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">pkl_file</span><span class="p">)</span> <span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">data1</span><span class="p">)</span> <span class="n">data2</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">pkl_file</span><span class="p">)</span> <span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">data2</span><span class="p">)</span> <span class="n">pkl_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> </pre></div> </div> <p>Here’s a larger example that shows how to modify pickling behavior for a class. The <tt class="xref py py-class docutils literal"><span class="pre">TextReader</span></tt> class opens a text file, and returns the line number and line contents each time its <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> method is called. If a <tt class="xref py py-class docutils literal"><span class="pre">TextReader</span></tt> instance is pickled, all attributes <em>except</em> the file object member are saved. When the instance is unpickled, the file is reopened, and reading resumes from the last location. The <a class="reference internal" href="#object.__setstate__" title="object.__setstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></tt></a> and <a class="reference internal" href="#object.__getstate__" title="object.__getstate__"><tt class="xref py py-meth docutils literal"><span class="pre">__getstate__()</span></tt></a> methods are used to implement this behavior.</p> <div class="highlight-python"><div class="highlight"><pre><span class="c">#!/usr/local/bin/python</span> <span class="k">class</span> <span class="nc">TextReader</span><span class="p">:</span> <span class="sd">"""Print and number lines in a text file."""</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">file</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="o">=</span> <span class="nb">file</span> <span class="bp">self</span><span class="o">.</span><span class="n">fh</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">lineno</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">def</span> <span class="nf">readline</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">lineno</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lineno</span> <span class="o">+</span> <span class="mi">1</span> <span class="n">line</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="p">:</span> <span class="k">return</span> <span class="bp">None</span> <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">):</span> <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="k">return</span> <span class="s">"</span><span class="si">%d</span><span class="s">: </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lineno</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span> <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">odict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="c"># copy the dict since we change it</span> <span class="k">del</span> <span class="n">odict</span><span class="p">[</span><span class="s">'fh'</span><span class="p">]</span> <span class="c"># remove filehandle entry</span> <span class="k">return</span> <span class="n">odict</span> <span class="k">def</span> <span class="nf">__setstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span> <span class="n">fh</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="nb">dict</span><span class="p">[</span><span class="s">'file'</span><span class="p">])</span> <span class="c"># reopen file</span> <span class="n">count</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">[</span><span class="s">'lineno'</span><span class="p">]</span> <span class="c"># read from file...</span> <span class="k">while</span> <span class="n">count</span><span class="p">:</span> <span class="c"># until line count is restored</span> <span class="n">fh</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span> <span class="n">count</span> <span class="o">=</span> <span class="n">count</span> <span class="o">-</span> <span class="mi">1</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="nb">dict</span><span class="p">)</span> <span class="c"># update attributes</span> <span class="bp">self</span><span class="o">.</span><span class="n">fh</span> <span class="o">=</span> <span class="n">fh</span> <span class="c"># save the file object</span> </pre></div> </div> <p>A sample usage might be something like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">TextReader</span> <span class="gp">>>> </span><span class="n">obj</span> <span class="o">=</span> <span class="n">TextReader</span><span class="o">.</span><span class="n">TextReader</span><span class="p">(</span><span class="s">"TextReader.py"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">obj</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span> <span class="go">'1: #!/usr/local/bin/python'</span> <span class="gp">>>> </span><span class="n">obj</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span> <span class="go">'2: '</span> <span class="gp">>>> </span><span class="n">obj</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span> <span class="go">'3: class TextReader:'</span> <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">pickle</span> <span class="gp">>>> </span><span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s">'save.p'</span><span class="p">,</span> <span class="s">'wb'</span><span class="p">))</span> </pre></div> </div> <p>If you want to see that <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> works across Python processes, start another Python session, before continuing. What follows can happen from either the same process or a new process.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">pickle</span> <span class="gp">>>> </span><span class="n">reader</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">'save.p'</span><span class="p">,</span> <span class="s">'rb'</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">reader</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span> <span class="go">'4: """Print and number lines in a text file."""'</span> </pre></div> </div> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt>Module <a class="reference internal" href="copy_reg.html#module-copy_reg" title="copy_reg: Register pickle support functions."><tt class="xref py py-mod docutils literal"><span class="pre">copy_reg</span></tt></a></dt> <dd>Pickle interface constructor registration for extension types.</dd> <dt>Module <a class="reference internal" href="shelve.html#module-shelve" title="shelve: Python object persistence."><tt class="xref py py-mod docutils literal"><span class="pre">shelve</span></tt></a></dt> <dd>Indexed databases of objects; uses <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a>.</dd> <dt>Module <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><tt class="xref py py-mod docutils literal"><span class="pre">copy</span></tt></a></dt> <dd>Shallow and deep object copying.</dd> <dt>Module <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a></dt> <dd>High-performance serialization of built-in types.</dd> </dl> </div> </div> </div> <div class="section" id="module-cPickle"> <span id="cpickle-a-faster-pickle"></span><h1>11.2. <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> — A faster <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a><a class="headerlink" href="#module-cPickle" title="Permalink to this headline">¶</a></h1> <p id="index-5">The <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module supports serialization and de-serialization of Python objects, providing an interface and functionality nearly identical to the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module. There are several differences, the most important being performance and subclassability.</p> <p>First, <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> can be up to 1000 times faster than <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> because the former is implemented in C. Second, in the <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module the callables <tt class="xref py py-func docutils literal"><span class="pre">Pickler()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">Unpickler()</span></tt> are functions, not classes. This means that you cannot use them to derive custom pickling and unpickling subclasses. Most applications have no need for this functionality and should benefit from the greatly improved performance of the <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module.</p> <p>The pickle data stream produced by <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> and <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> are identical, so it is possible to use <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> and <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> interchangeably with existing pickles. <a class="footnote-reference" href="#id20" id="id10">[10]</a></p> <p>There are additional minor differences in API between <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> and <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a>, however for most applications, they are interchangeable. More documentation is provided in the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module documentation, which includes a list of the documented differences.</p> <p class="rubric">Footnotes</p> <table class="docutils footnote" frame="void" id="id11" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Don’t confuse this with the <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a> module</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id12" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>In the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module these callables are classes, which you could subclass to customize the behavior. However, in the <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module these callables are factory functions and so cannot be subclassed. One common reason to subclass is to control what objects can actually be unpickled. See section <a class="reference internal" href="#pickle-sub"><em>Subclassing Unpicklers</em></a> for more details.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id13" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id3">[3]</a></td><td><em>Warning</em>: this is intended for pickling multiple objects without intervening modifications to the objects or their parts. If you modify an object and then pickle it again using the same <tt class="xref py py-class docutils literal"><span class="pre">Pickler</span></tt> instance, the object is not pickled again — a reference to it is pickled and the <tt class="xref py py-class docutils literal"><span class="pre">Unpickler</span></tt> will return the old value, not the modified one. There are two problems here: (1) detecting changes, and (2) marshalling a minimal set of changes. Garbage Collection may also become a problem here.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id14" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id4">[4]</a></td><td>The exception raised will likely be an <a class="reference internal" href="exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a> or an <a class="reference internal" href="exceptions.html#exceptions.AttributeError" title="exceptions.AttributeError"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a> but it could be something else.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id15" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id5">[5]</a></td><td>These methods can also be used to implement copying class instances.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id16" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id6">[6]</a></td><td>This protocol is also used by the shallow and deep copying operations defined in the <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><tt class="xref py py-mod docutils literal"><span class="pre">copy</span></tt></a> module.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id17" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id7">[7]</a></td><td>The actual mechanism for associating these user defined functions is slightly different for <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> and <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a>. The description given here works the same for both implementations. Users of the <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> module could also use subclassing to effect the same results, overriding the <tt class="xref py py-meth docutils literal"><span class="pre">persistent_id()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">persistent_load()</span></tt> methods in the derived classes.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id18" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id8">[8]</a></td><td>We’ll leave you with the image of Guido and Jim sitting around sniffing pickles in their living rooms.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id19" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id9">[9]</a></td><td>A word of caution: the mechanisms described here use internal attributes and methods, which are subject to change in future versions of Python. We intend to someday provide a common interface for controlling this behavior, which will work in either <a class="reference internal" href="#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> or <a class="reference internal" href="#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a>.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id20" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id10">[10]</a></td><td>Since the pickle data format is actually a tiny stack-oriented programming language, and some freedom is taken in the encodings of certain objects, it is possible that the two modules produce different data streams for the same input objects. However it is guaranteed that they will always be able to read each other’s data streams.</td></tr> </tbody> </table> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">11.1. <tt class="docutils literal"><span class="pre">pickle</span></tt> — Python object serialization</a><ul> <li><a class="reference internal" href="#relationship-to-other-python-modules">11.1.1. Relationship to other Python modules</a></li> <li><a class="reference internal" href="#data-stream-format">11.1.2. Data stream format</a></li> <li><a class="reference internal" href="#usage">11.1.3. Usage</a></li> <li><a class="reference internal" href="#what-can-be-pickled-and-unpickled">11.1.4. What can be pickled and unpickled?</a></li> <li><a class="reference internal" href="#the-pickle-protocol">11.1.5. The pickle protocol</a><ul> <li><a class="reference internal" href="#pickling-and-unpickling-normal-class-instances">11.1.5.1. Pickling and unpickling normal class instances</a></li> <li><a class="reference internal" href="#pickling-and-unpickling-extension-types">11.1.5.2. Pickling and unpickling extension types</a></li> <li><a class="reference internal" href="#pickling-and-unpickling-external-objects">11.1.5.3. Pickling and unpickling external objects</a></li> </ul> </li> <li><a class="reference internal" href="#subclassing-unpicklers">11.1.6. Subclassing Unpicklers</a></li> <li><a class="reference internal" href="#example">11.1.7. Example</a></li> </ul> </li> <li><a class="reference internal" href="#module-cPickle">11.2. <tt class="docutils literal"><span class="pre">cPickle</span></tt> — A faster <tt class="docutils literal"><span class="pre">pickle</span></tt></a></li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="persistence.html" title="previous chapter">11. Data Persistence</a></p> <h4>Next topic</h4> <p class="topless"><a href="copy_reg.html" title="next chapter">11.3. <tt class="docutils literal"><span class="pre">copy_reg</span></tt> — Register <tt class="docutils literal"><span class="pre">pickle</span></tt> support functions</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/pickle.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="copy_reg.html" title="11.3. copy_reg — Register pickle support functions" >next</a> |</li> <li class="right" > <a href="persistence.html" title="11. Data Persistence" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="persistence.html" >11. Data Persistence</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2020, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Oct 13, 2020. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close