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.

Author JelleZijlstra
Recipients JelleZijlstra, alexandre.vassalotti, docs@python
Date 2022-04-03.15:10:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1648998659.8.0.245772274253.issue47206@roundup.psfhosted.org>
In-reply-to
Content
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-03 15:10:59JelleZijlstrasetrecipients: + JelleZijlstra, alexandre.vassalotti, docs@python
2022-04-03 15:10:59JelleZijlstrasetmessageid: <1648998659.8.0.245772274253.issue47206@roundup.psfhosted.org>
2022-04-03 15:10:59JelleZijlstralinkissue47206 messages
2022-04-03 15:10:59JelleZijlstracreate