This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pickle docs are wrong about nested classes
Type: behavior Stage:
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: JelleZijlstra, alexandre.vassalotti, docs@python, maggyero
Priority: normal Keywords:

Created on 2022-04-03 15:10 by JelleZijlstra, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg416625 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-04-03 15:10
https://docs.python.org/3.10/library/pickle.html#what-can-be-pickled-and-unpickled says that only "classes that are defined at the top level of a module" can be pickled. But in fact these work fine in current Python, probably since 3.3 when __qualname__ was added (https://docs.python.org/3/library/stdtypes.html#definition.__qualname__). Similarly, the docs claim only top-level functions can be pickled, but in fact methods nested in classes work fine.

Example script demonstrating that these work:

import pickle


class X:
    class Y:
        pass

    def method(self):
        pass


print(pickle.dumps(X.Y))
print(pickle.loads(pickle.dumps(X.Y)))

print(pickle.dumps(X.Y()))
print(pickle.loads(pickle.dumps(X.Y())))

print(pickle.dumps(X.method))
print(pickle.loads(pickle.dumps(X.method)))
History
Date User Action Args
2022-04-11 14:59:58adminsetgithub: 91362
2022-04-04 16:30:12maggyerosetnosy: + maggyero
2022-04-03 15:10:59JelleZijlstracreate