[New-bugs-announce] [issue47206] pickle docs are wrong about nested classes

Jelle Zijlstra report at bugs.python.org
Sun Apr 3 11:10:59 EDT 2022


New submission from Jelle Zijlstra <jelle.zijlstra at gmail.com>:

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)))

----------
assignee: docs at python
components: Documentation
messages: 416625
nosy: JelleZijlstra, alexandre.vassalotti, docs at python
priority: normal
severity: normal
status: open
title: pickle docs are wrong about nested classes
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47206>
_______________________________________


More information about the New-bugs-announce mailing list