Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 710280b

Browse files
committed
Issue #18840: Introduce the json module in the tutorial, and deemphasize the pickle module.
2 parents 74e7cf3 + dd799d2 commit 710280b

3 files changed

Lines changed: 70 additions & 34 deletions

File tree

Doc/glossary.rst

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ Glossary
7878
Benevolent Dictator For Life, a.k.a. `Guido van Rossum
7979
<http://www.python.org/~guido/>`_, Python's creator.
8080

81+
binary file
82+
A :term:`file object` able to read and write
83+
:term:`bytes-like objects <bytes-like object>`.
84+
85+
.. seealso::
86+
A :term:`text file` reads and writes :class:`str` objects.
87+
8188
bytes-like object
8289
An object that supports the :ref:`bufferobjects`, like :class:`bytes`,
8390
:class:`bytearray` or :class:`memoryview`. Bytes-like objects can
@@ -225,10 +232,11 @@ Glossary
225232
etc.). File objects are also called :dfn:`file-like objects` or
226233
:dfn:`streams`.
227234

228-
There are actually three categories of file objects: raw binary files,
229-
buffered binary files and text files. Their interfaces are defined in the
230-
:mod:`io` module. The canonical way to create a file object is by using
231-
the :func:`open` function.
235+
There are actually three categories of file objects: raw
236+
:term:`binary files <binary file>`, buffered
237+
:term:`binary files <binary file>` and :term:`text files <text file>`.
238+
Their interfaces are defined in the :mod:`io` module. The canonical
239+
way to create a file object is by using the :func:`open` function.
232240

233241
file-like object
234242
A synonym for :term:`file object`.
@@ -800,6 +808,14 @@ Glossary
800808
:meth:`~collections.somenamedtuple._asdict`. Examples of struct sequences
801809
include :data:`sys.float_info` and the return value of :func:`os.stat`.
802810

811+
text file
812+
A :term:`file object` able to read and write :class:`str` objects.
813+
Often, a text file actually accesses a byte-oriented datastream
814+
and handles the text encoding automatically.
815+
816+
.. seealso::
817+
A :term:`binary file` reads and write :class:`bytes` objects.
818+
803819
triple-quoted string
804820
A string which is bound by three instances of either a quotation mark
805821
(") or an apostrophe ('). While they don't provide any functionality

Doc/tutorial/inputoutput.rst

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -377,47 +377,64 @@ File objects have some additional methods, such as :meth:`~file.isatty` and
377377
Reference for a complete guide to file objects.
378378

379379

380-
.. _tut-pickle:
380+
.. _tut-json:
381381

382-
The :mod:`pickle` Module
383-
------------------------
382+
Saving structured data with :mod:`json`
383+
---------------------------------------
384384

385-
.. index:: module: pickle
385+
.. index:: module: json
386386

387-
Strings can easily be written to and read from a file. Numbers take a bit more
387+
Strings can easily be written to and read from a file. Numbers take a bit more
388388
effort, since the :meth:`read` method only returns strings, which will have to
389389
be passed to a function like :func:`int`, which takes a string like ``'123'``
390-
and returns its numeric value 123. However, when you want to save more complex
391-
data types like lists, dictionaries, or class instances, things get a lot more
392-
complicated.
393-
394-
Rather than have users be constantly writing and debugging code to save
395-
complicated data types, Python provides a standard module called :mod:`pickle`.
396-
This is an amazing module that can take almost any Python object (even some
397-
forms of Python code!), and convert it to a string representation; this process
398-
is called :dfn:`pickling`. Reconstructing the object from the string
399-
representation is called :dfn:`unpickling`. Between pickling and unpickling,
400-
the string representing the object may have been stored in a file or data, or
390+
and returns its numeric value 123. When you want to save more complex data
391+
types like nested lists and dictionaries, parsing and serializing by hand
392+
becomes complicated.
393+
394+
Rather than having users constantly writing and debugging code to save
395+
complicated data types to files, Python allows you to use the popular data
396+
interchange format called `JSON (JavaScript Object Notation)
397+
<http://json.org>`_. The standard module called :mod:`json` can take Python
398+
data hierarchies, and convert them to string representations; this process is
399+
called :dfn:`serializing`. Reconstructing the data from the string representation
400+
is called :dfn:`deserializing`. Between serializing and deserializing, the
401+
string representing the object may have been stored in a file or data, or
401402
sent over a network connection to some distant machine.
402403

403-
If you have an object ``x``, and a file object ``f`` that's been opened for
404-
writing, the simplest way to pickle the object takes only one line of code::
404+
.. note::
405+
The JSON format is commonly used by modern applications to allow for data
406+
exchange. Many programmers are already familiar with it, which makes
407+
it a good choice for interoperability.
405408

406-
pickle.dump(x, f)
409+
If you have an object ``x``, you can view its JSON string representation with a
410+
simple line of code::
407411

408-
To unpickle the object again, if ``f`` is a file object which has been opened
409-
for reading::
412+
>>> json.dumps([1, 'simple', 'list'])
413+
'[1, "simple", "list"]'
410414

411-
x = pickle.load(f)
415+
Another variant of the :func:`~json.dumps` function, called :func:`~json.dump`,
416+
simply serializes the object to a :term:`text file`. So if ``f`` is a
417+
:term:`text file` object opened for writing, we can do this::
412418

413-
(There are other variants of this, used when pickling many objects or when you
414-
don't want to write the pickled data to a file; consult the complete
415-
documentation for :mod:`pickle` in the Python Library Reference.)
419+
json.dump(x, f)
416420

417-
:mod:`pickle` is the standard way to make Python objects which can be stored and
418-
reused by other programs or by a future invocation of the same program; the
419-
technical term for this is a :dfn:`persistent` object. Because :mod:`pickle` is
420-
so widely used, many authors who write Python extensions take care to ensure
421-
that new data types such as matrices can be properly pickled and unpickled.
421+
To decode the object again, if ``f`` is a :term:`text file` object which has
422+
been opened for reading::
422423

424+
x = json.load(f)
425+
426+
This simple serialization technique can handle lists and dictionaries, but
427+
serializing arbitrary class instances in JSON requires a bit of extra effort.
428+
The reference for the :mod:`json` module contains an explanation of this.
429+
430+
.. seealso::
431+
432+
:mod:`pickle` - the pickle module
433+
434+
Contrary to :ref:`JSON <tut-json>`, *pickle* is a protocol which allows
435+
the serialization of arbitrarily complex Python objects. As such, it is
436+
specific to Python and cannot be used to communicate with applications
437+
written in other languages. It is also insecure by default:
438+
deserializing pickle data coming from an untrusted source can execute
439+
arbitrary code, if the data was crafted by a skilled attacker.
423440

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ Build
103103
Documentation
104104
-------------
105105

106+
- Issue #18840: Introduce the json module in the tutorial, and deemphasize
107+
the pickle module.
108+
106109
- Issue #19845: Updated the Compiling Python on Windows section.
107110

108111
- Issue #19795: Improved markup of True/False constants.

0 commit comments

Comments
 (0)