Python AttributeError:Edge实例没有属性';vto&x27;

Python AttributeError:Edge实例没有属性';vto&x27;,python,pickle,Python,Pickle,我正在尝试pickle一个类实例,其中包含另一个实例的两个列表。两个列表中的实例具有相互引用实例的属性。这些是课程 class Graph: def __init__(self): self.vertices = {} self.edges = set() def __repr__(self): return "\n".join(map(str, sorted(self.vertices, key=lambda v:v.id)))

我正在尝试pickle一个类实例,其中包含另一个实例的两个列表。两个列表中的实例具有相互引用实例的属性。这些是课程

class Graph:
    def __init__(self):
        self.vertices = {}
        self.edges = set()
    def __repr__(self):
        return "\n".join(map(str, sorted(self.vertices, key=lambda v:v.id)))

class Edge:
    def __init__(self, vfrom, vto):
        self.vfrom = vfrom
        self.vto = vto
    def __hash__(self):
        return hash(tuple(map(hash, (self.vto, self.vfrom))))
    def __repr__(self):
        return str(self.vto.id)

class Vertax:
    def __init__(self, id):
        self.id = id
        self.incoming = set()
        self.outgoing = set()
    def __repr__(self):
        return "Vertax %d -> %s"%(self.id, ", ".join(map(str, self.outgoing)))
    def __hash__(self):
        return hash(self.id)
当我试图绘制一个简单的图形时,取消绘制会产生一个错误

>>> v0 = Vertax(0)
>>> v1 = Vertax(1)
>>> e0to1 = Edge(v0, v1)
>>> v0.outgoing.add(e0to1)
>>> v1.incoming.add(e0to1)
>>> g = Graph()
>>> g.vertices[v0] = v0
>>> g.vertices[v1] = v1
>>> g.edges.add(e0to1)
>>> print g
Vertax 0 -> 1
Vertax 1 -> 
>>> 
>>> import pickle
>>> p = pickle.dumps(g)
>>> pickle.loads(p)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/pickle.py", line 1374, in loads
    return Unpickler(file).load()
  File "/usr/lib/python2.6/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.6/pickle.py", line 1133, in load_reduce
    value = func(*args)
  File "<stdin>", line 6, in __hash__
AttributeError: Edge instance has no attribute 'vto'
>v0=Vertax(0)
>>>v1=顶点(1)
>>>e0to1=边缘(v0,v1)
>>>v0.输出。添加(e0to1)
>>>v1.传入.添加(e0to1)
>>>g=图()
>>>g.顶点[v0]=v0
>>>g.顶点[v1]=v1
>>>g.边缘添加(e0to1)
>>>打印g
顶点0->1
Vertax 1->
>>> 
>>>进口泡菜
>>>p=酸洗倾倒量(g)
>>>酸洗负荷(p)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib/python2.6/pickle.py”,第1374行,装入
返回Unpickler(file.load())
文件“/usr/lib/python2.6/pickle.py”,第858行,已加载
调度[键](自身)
文件“/usr/lib/python2.6/pickle.py”,第1133行,在load\u reduce中
值=func(*args)
文件“”,第6行,在散列中__
AttributeError:边缘实例没有属性“vto”

我发现,如果注释掉Edge类的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
函数,错误就会消失。我需要您的帮助来理解为什么会发生这种情况。

这个Python问题可能是原因:


“pickle-无法使用自定义
\uuu hash\uuuuuu
”是不是“vertax”是顶点的非美式拼写?与其做
散列(tuple(map(hash,(self.vto,self.vfrom))
,不如做
散列((self.vto,self.vfrom))
-
散列(tuple)。\uu hash\uuu,所以这两者实际上是等价的。哦,这是我的错误。克里斯·摩根//谢谢,我不知道。这就是我想要找到的。非常感谢。:)