Note that there are some explanatory texts on larger screens.

plurals
  1. POAttributeError: Edge instance has no attribute 'vto'
    text
    copied!<p>I'm trying to pickle a class instance containing two lists of another instances. The instances in the two lists have attributes that refer instances of each other. Here are the classes.</p> <pre><code>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 -&gt; %s"%(self.id, ", ".join(map(str, self.outgoing))) def __hash__(self): return hash(self.id) </code></pre> <p>When I tried to pickle a simple graph, unpickling gives an error.</p> <pre><code>&gt;&gt;&gt; v0 = Vertax(0) &gt;&gt;&gt; v1 = Vertax(1) &gt;&gt;&gt; e0to1 = Edge(v0, v1) &gt;&gt;&gt; v0.outgoing.add(e0to1) &gt;&gt;&gt; v1.incoming.add(e0to1) &gt;&gt;&gt; g = Graph() &gt;&gt;&gt; g.vertices[v0] = v0 &gt;&gt;&gt; g.vertices[v1] = v1 &gt;&gt;&gt; g.edges.add(e0to1) &gt;&gt;&gt; print g Vertax 0 -&gt; 1 Vertax 1 -&gt; &gt;&gt;&gt; &gt;&gt;&gt; import pickle &gt;&gt;&gt; p = pickle.dumps(g) &gt;&gt;&gt; pickle.loads(p) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; 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 "&lt;stdin&gt;", line 6, in __hash__ AttributeError: Edge instance has no attribute 'vto' </code></pre> <p>I found that the error disappears if one comments out the <code>__hash__</code> function of the Edge class. I need your help to understand why this happens.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload