Python key error - for key in dictionary: dictionary[key] -


i programming graph class handle pathing in game. graphs have 1 field, mapping, dictionary maps nodes edges. in 1 of functions, have following bit of code:

for key in self.mapping:     connected_edges = self.mapping[key] 

at 1 point in program, call code , following error:

keyerror: <node.node instance @ 0x00000000026f3088> 

how possible? i'm looping through keys in dictionary, how can key not in it? sanity check, took length of dictionary , saw 5, ie: not 0. ideas? thanks!

i implement own eq , hash method both node , edge. here parts of classes:

class node:     """     node connects other nodes via edges form graph      each node has unique identifier (id) can mathematically distinct. optionally, node has x ,     y coordinate.     """     n_nodes = 0     def __init__(self, x=0, y=0):         """         create node own unique identifier.          :param x: x coordinate         :param y: y coordinate         :return: initialized node object         """         node.n_nodes += 1         self.id = node.n_nodes         self.x = x  # x position         self.y = y  # y position      def __eq__(self, other):         return (self.id, self.x, self.y) == (other.id, other.x, other.y)      def __hash__(self):         return hash((self.id, self.x, self.y))    class edge:     """     edge connection between 2 nodes.      edges have unique identifier , list of 2 nodes. optionally, edge can have numerical weight. edges can     directed, in case possible traverse edge in 1 direction, not both.     """     n_edges = 0     def __init__(self, node1, node2, weight=1, directed=false):         """         create edge own unique identifier          :param node1: node @ first end of edge         :param node2: node @ second end of edge         :param weight: numerical weight of connection between node1 , node2         :param directed: if true, edge oriented node1 node 2         :return: initialized edge object         """         edge.n_edges += 1         self.id = edge.n_edges         self.weight = weight         self.nodes = frozenset([node1, node2])         self.directed = directed      def __eq__(self, other):         return (self.id, self.weight, self.nodes, self.directed) == (other.id, other.weight, other.nodes, other.directed)      def __hash__(self):         return hash((self.id, self.weight, self.nodes, self.directed)) 

if objects implement __eq__ , __hash__, have make sure hash value not change after object created. if mutate objects after creating them, can them inconsistent state hash value stored them in dictionary inconsistent current hash value. here's example:

class foo(object):     def __init__(self, x):         self.x = x     def __eq__(self, other):         return self.x == other.x     def __hash__(self):         return self.x  a, b, c = foo(1), foo(2), foo(3)  x = {a: 1, b: 2, c: 3}  key in x:     print(x[key])  a.x = 100  key in x:     print(x[key]) 

the result keyerror similar 1 received. presumably mutating node objects somewhere along line.

mutable objects cannot hashed (or @ least, hash value cannot depend on of mutable state).


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -