Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
41796918
Kaydet (Commit)
41796918
authored
Tem 02, 1999
tarafından
Fred Drake
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fix a few markup nits, improve some index entries.
üst
7d807795
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
38 deletions
+41
-38
libpickle.tex
Doc/lib/libpickle.tex
+41
-38
No files found.
Doc/lib/libpickle.tex
Dosyayı görüntüle @
41796918
...
...
@@ -12,29 +12,30 @@
\indexii
{
pickling
}{
objects
}
The
\module
{
pickle
}
module implements a basic but powerful algorithm
for
``pickling'' (a.k.a.
\
serializing, marshalling or flattening) nearly
arbitrary Python objects. This is the act of converting objects to a
stream of bytes (and back: ``unpickling'').
This is a more primitive notion than
persistency --- although
\module
{
pickle
}
reads and writes file objects,
it does not handle the issue of naming persistent objects, nor the
(even more complicated) area of concurrent access to persistent
objects. The
\module
{
pickle
}
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. 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
The
\module
{
pickle
}
module implements a basic but powerful algorithm
for ``pickling'' (a.k.a.
\
serializing, marshalling or flattening)
nearly arbitrary Python objects. This is the act of converting
objects to a stream of bytes (and back: ``unpickling''). This is a
more primitive notion than persistency --- although
\module
{
pickle
}
reads and writes file objects, it does not handle the issue of naming
persistent objects, nor the (even more complicated) area of concurrent
access to persistent objects. The
\module
{
pickle
}
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. 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
\refmodule
{
shelve
}
\refstmodindex
{
shelve
}
provides a simple interface
to pickle and unpickle objects on DBM-style database files.
\strong
{
Note:
}
The
\module
{
pickle
}
module is rather slow. A
reimplementation of the same algorithm in C, which is up to 1000 times
faster, is available as the
\refmodule
{
cPickle
}
\refbimodindex
{
cPickle
}
module. This has the same interface except that
\code
{
Pickler
}
and
\code
{
Unpickler
}
are factory functions, not classes (so they cannot be
used as base classes for inheritance).
faster, is available as the
\refmodule
{
cPickle
}
\refbimodindex
{
cPickle
}
module. This has the same
interface except that
\class
{
Pickler
}
and
\class
{
Unpickler
}
are
factory functions, not classes (so they cannot be used as base classes
for inheritance).
Unlike the built-in module
\refmodule
{
marshal
}
\refbimodindex
{
marshal
}
,
\module
{
pickle
}
handles the following correctly:
...
...
@@ -72,12 +73,11 @@ compatibility with the Python 1.4 pickle module. In a future version,
the default may change to binary.
The
\module
{
pickle
}
module doesn't handle code objects, which the
\refmodule
{
marshal
}
module does. I suppose
\module
{
pickle
}
could, and maybe
it should, but there's probably no great need for it right now (as
long as
\refmodule
{
marshal
}
continues to be used for reading and writing
code objects), and at least this avoids the possibility of smuggling
Trojan horses into a program.
\refbimodindex
{
marshal
}
\refmodule
{
marshal
}
\refbimodindex
{
marshal
}
module does. I suppose
\module
{
pickle
}
could, and maybe it should, but there's probably no
great need for it right now (as long as
\refmodule
{
marshal
}
continues
to be used for reading and writing code objects), and at least this
avoids the possibility of smuggling Trojan horses into a program.
For the benefit of persistency modules written using
\module
{
pickle
}
, it
supports the notion of a reference to an object outside the pickled
...
...
@@ -109,10 +109,15 @@ which should return a \emph{tuple} containing the arguments to be
passed to the class constructor (
\method
{__
init
__
()
}
). This method is
called at pickle time; the tuple it returns is incorporated in the
pickle for the instance.
\ttindex
{__
getinitargs
__
()
}
\ttindex
{__
init
__
()
}
Classes can further influence how their instances are pickled --- if the class
\withsubitem
{
(copy protocol)
}{
\ttindex
{__
getinitargs
__
()
}}
\withsubitem
{
(instance constructor)
}{
\ttindex
{__
init
__
()
}}
Classes can further influence how their instances are pickled --- if
the class
\withsubitem
{
(copy protocol)
}{
\ttindex
{__
getstate
__
()
}
\ttindex
{__
setstate
__
()
}}
\withsubitem
{
(instance attribute)
}{
\ttindex
{__
dict
__}}
defines the method
\method
{__
getstate
__
()
}
, it is called and the return
state is pickled as the contents for the instance, and if the class
defines the method
\method
{__
setstate
__
()
}
, it is called with the
...
...
@@ -126,9 +131,6 @@ and \method{__setstate__()}, the state object needn't be a dictionary
--- these methods can do what they want.) This protocol is also used
by the shallow and deep copying operations defined in the
\refmodule
{
copy
}
\refstmodindex
{
copy
}
module.
\ttindex
{__
getstate
__
()
}
\ttindex
{__
setstate
__
()
}
\ttindex
{__
dict
__}
Note that when class instances are pickled, their class's code and
data are not pickled along with them. Only the instance data are
...
...
@@ -175,12 +177,12 @@ x = pickle.load(f)
\end{verbatim}
The
\class
{
Pickler
}
class only calls the method
\code
{
f.write()
}
with a
\withsubitem
{
(class in pickle)
}{
\ttindex
{
Unpickler
}
\ttindex
{
Pickler
}}
string argument. The
\class
{
Unpickler
}
calls the methods
\code
{
f.read()
}
(with an integer argument) and
\code
{
f.readline()
}
(without argument),
both returning a string. It is explicitly allowed to pass non-file
objects here, as long as they have the right methods.
\ttindex
{
Unpickler
}
\ttindex
{
Pickler
}
The constructor for the
\class
{
Pickler
}
class has an optional second
argument,
\var
{
bin
}
. If this is present and nonzero, the binary
...
...
@@ -190,6 +192,7 @@ but backwards compatible) text pickle format is used. The
between binary and text pickle formats; it accepts either format.
The following types can be pickled:
\begin{itemize}
\item
\code
{
None
}
...
...
@@ -257,7 +260,7 @@ the string past the pickled object's representation are ignored.
\begin{excdesc}
{
PicklingError
}
This exception is raised when an unpicklable object is passed to
\
code
{
Pickler.dump()
}
.
\
method
{
Pickler.dump()
}
.
\end{excdesc}
...
...
@@ -283,11 +286,11 @@ This exception is raised when an unpicklable object is passed to
The
\module
{
cPickle
}
module provides a similar interface and identical
functionality as the
\refmodule
{
pickle
}
module, but can be up to 1000
times faster since it is implemented in C. The only other
important difference to note is that
\function
{
Pickler()
}
and
\function
{
Unpickler()
}
are functions and not classes, and so cannot be
subclassed. This should not be an issue in most cases.
functionality as the
\refmodule
{
pickle
}
\refstmodindex
{
pickle
}
module,
but can be up to 1000 times faster since it is implemented in C. The
only other important difference to note is that
\function
{
Pickler()
}
and
\function
{
Unpickler()
}
are functions and not classes, and so
cannot be
subclassed. This should not be an issue in most cases.
The format of the pickle data is identical to that produced using the
\refmodule
{
pickle
}
module, so it is possible to use
\refmodule
{
pickle
}
and
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment