python – AttributeError:边缘实例没有属性’vto’
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – AttributeError:边缘实例没有属性’vto’,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1616字,纯文字阅读大概需要3分钟。
内容图文
![python – AttributeError:边缘实例没有属性’vto’](/upload/InfoBanner/zyjiaocheng/783/41b937659f2749bf962c797809332f86.jpg)
我正在尝试挑选一个包含两个另一个实例列表的类实例.两个列表中的实例具有引用彼此实例的属性.这是课程.
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)
当我试图挑选一个简单的图形时,unpickling会出错.
>>> 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'
我发现如果注释掉Edge类的__hash__函数,错误就会消失.我需要你的帮助才能理解为什么会这样.
解决方法:
这个Python问题可能是原因:http://bugs.python.org/issue1761028
“pickle – 不能用自定义的__hash__来破坏圆形deps”
内容总结
以上是互联网集市为您收集整理的python – AttributeError:边缘实例没有属性’vto’全部内容,希望文章能够帮你解决python – AttributeError:边缘实例没有属性’vto’所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。