diff -r 537e13ca7683 Doc/library/pickle.rst --- a/Doc/library/pickle.rst Thu Oct 17 23:27:17 2013 +1000 +++ b/Doc/library/pickle.rst Thu Oct 17 07:24:40 2013 -0700 @@ -377,39 +377,40 @@ What can be pickled and unpickled? The following types can be pickled: * ``None``, ``True``, and ``False`` * integers, floating point numbers, complex numbers * strings, bytes, bytearrays * tuples, lists, sets, and dictionaries containing only picklable objects -* functions defined at the top level of a module +* functions defined at the top level of a module (using :keyword:`def`, not + :keyword:`lambda`) * built-in functions defined at the top level of a module * classes that are defined at the top level of a module * instances of such classes whose :attr:`~object.__dict__` or the result of calling :meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for details). Attempts to pickle unpicklable objects will raise the :exc:`PicklingError` 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 :exc:`RuntimeError` will be raised in this case. You can carefully raise this limit with :func:`sys.setrecursionlimit`. 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 +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. [#]_ 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 ``attr`` is not restored in the unpickling environment:: @@ -843,20 +844,23 @@ The following example reads the resultin Shallow and deep object copying. Module :mod:`marshal` High-performance serialization of built-in types. .. rubric:: Footnotes .. [#] Don't confuse this with the :mod:`marshal` module +.. [#] This is why :keyword:`lambda` functions cannot be pickled: all + :keyword:`lambda` functions share the same name: ````. + .. [#] The exception raised will likely be an :exc:`ImportError` or an :exc:`AttributeError` but it could be something else. .. [#] The :mod:`copy` module uses this protocol for shallow and deep copying operations. .. [#] The limitation on alphanumeric characters is due to the fact the persistent IDs, in protocol 0, are delimited by the newline character. Therefore if any kind of newline characters occurs in persistent IDs, the resulting pickle will become unreadable.