2

我有一个对象:

class X():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

其属性 c 是列表对象(不同种类)的一个:

class Y():
    def __init__(self, x, y):
        self.x = x
        self.y = y

我腌制如下:

import pickle
pickle.dump(instance_of_class_X,open(dir, "wb"))

我加载如下:

import pickle
from some_library import X, Y # I import the two classes involved
pickle.load(open(dir,"rb"))

我收到以下错误:

AttributeError:“模块”对象没有属性“Y”

不知道该怎么做,任何帮助将不胜感激。

4

1 回答 1

4

您可能会因为需要pickle通过与最初使用的完全限定名称相同的名称来使用该类。这取决于您最初创建对象时 X 和 Y 所在的命名空间。请参阅此答案此页面

于 2012-06-10T19:41:36.690 回答