Home > Blockchain >  How to modify single object inside dict values stored as a set?
How to modify single object inside dict values stored as a set?

Time:09-13

I have a dictionary which represents graph. Key is Node class and values are set of Nodes. So it has this structure: dict[Node] = {Node, Node, Node}

class Node:

def __init__(self, row, column, region):
    self.id = f'{str(row)}-{str(column)}'
    self.region = region
    self.visited = False

In code below I need to update visited property of Node class.

        while nodes_queue:
        current_node = nodes_queue.pop()

        for edge in self.map[current_node]: 
            if edge.region == root.region and not edge.visited:
                edge.visited = True # Not updated!
                nodes_queue.append(edge)

But it looks like I get view of Node objects instead of actual objects. When I update visited property in for loop and get it from next iteration, the property is still set to False

CodePudding user response:

I've figured it out. I was storing different Node objects as key and what was in values set in my dictionary. I created context of all Nodes and get Node from there by its id.

def get_node_from_context(self, row, column, region):
        node = Node(row, column, region)

        if node not in self.__graph_context__:
            self.__graph_context__[node] = node
        else:
            node = self.__graph_context__[node]

        return node
  • Related