顶层 类 可以 pickle 和 unpickled(文档错误?)

Can top level classes be pickled and unpickled (documentation wrong?)

下面链接的文档似乎说顶级 classes 及其实例可以被 pickle。但是根据我之前的回答,好像不对。在我发布的脚本中,pickle 接受 class 对象并写入一个文件,但这没有用。

这是我的问题:此文档是否有误,还是有更微妙的地方我不明白?此外,在这种情况下,pickle 是否应该生成某种错误消息?

https://docs.python.org/2/library/pickle.html#what-can-be-pickled-and-unpickled,

The following types can be pickled:

  • None, True, and False
  • integers, long integers, floating point numbers, complex numbers
  • normal and Unicode strings
  • tuples, lists, sets, and dictionaries containing only picklable objects
  • functions defined at the top level of a module
  • built-in functions defined at the top level of a module
  • classes that are defined at the top level of a module ( my bold )
  • instances of such classes whose dict or the result of calling getstate() > is picklable (see section The pickle protocol for details).

创建一个class,即定义在模块的顶层:

foo.py:

class Foo(object): pass

然后运行一个单独的脚本,

script.py:

import pickle
import foo


with open('/tmp/out.pkl', 'w') as f:
    pickle.dump(foo.Foo, f)

del foo

with open('/tmp/out.pkl', 'r') as f:
    cls = pickle.load(f)

print(cls)

打印

<class 'foo.Foo'>

请注意,pickle 文件 out.pkl 仅包含 字符串 ,它命名定义模块和 class 的名称。它不存储 class:

的定义
cfoo
Foo
p0
.

因此,在 unpickling 时 定义模块 foo 必须包含 class 的定义。如果从定义模块

中删除 class
del foo.Foo

然后你会得到错误

AttributeError: 'module' object has no attribute 'Foo'

完全有可能在 python 中 pickle 一个 class 实例…同时保存代码以重建 class 和实例的状态。如果您想在 pickle 之上拼凑出一个解决方案,或者使用基于 "trojan horse" exec 的方法,请按以下步骤操作:

How to unpickle an object whose class exists in a different namespace (python)?

或者,如果您使用 dill,您的 dump 函数已经知道如何存储 class 实例、class 代码和实例状态:

Pickle python class instance plus definition

我是 dill 的作者,我创建 dill 的部分原因是为了能够在 multiprocessing 中发布 class 实例和 class 方法.

Can't pickle <type 'instancemethod'> when using python's multiprocessing Pool.map()