Home > Net >  How to Get Value Stored in Memory Instead of Memory Object
How to Get Value Stored in Memory Instead of Memory Object

Time:03-01

I am learning Graphs and have a simple Graph Class with Node and Edge Objects. Each Edge has a Value, Node_From, and Node_To. When I go to print an edge list, however, the Value comes out as the integer I want, but the Node_From and Node_To values come out as the memory object. How do I get the value stored at that memory object? Relevant code follows, then output.

class Node(object):
    def __init__(self, value):
        self.value = value
        self.edges = []

class Edge(object):
    def __init__(self, value, node_from, node_to):
        self.value = value
        self.node_from = node_from
        self.node_to = node_to

class Graph(object):
    def __init__(self, nodes=[], edges=[]):
        self.nodes = nodes
        self.edges = edges

    def get_edge_list(self):
        """Don't return a list of edge objects!
        Return a list of triples that looks like this:
        (Edge Value, From Node Value, To Node Value)"""
        triples = []
        for edge in self.edges:
            triples.append((edge.value, edge.node_from, edge.node_to))
        return triples

Output of print(graph.get_edge_list()):

[(100, <graph_representation.Node object at 0x7ffbee5f7750>, <graph_representation.Node object at 0x7ffbee5f7790>), (101, <graph_representation.Node object at 0x7ffbee5f7750>, <graph_representation.Node object at 0x7ffbee5f7810>), (102, <graph_representation.Node object at 0x7ffbee5f7750>, <graph_representation.Node object at 0x7ffbee5f7890>), (103, <graph_representation.Node object at 0x7ffbee5f7810>, <graph_representation.Node object at 0x7ffbee5f7890>)]

The 100, 101, 102, etc. are the values that I want and correct, but I also want the values for the node_from and node_to parts of each Edge object, not the memory location for each. How do I turn the <graph_representation.Node object at 0x7ffbee5f7750> into whatever value is stored at that memory location? Thanks!

CodePudding user response:

What you're seeing is the default output for an object that hasn't overwritten either the str() method or the repr() method.

You could get what you want by adding this to your Node class:

class Node(object):
    def __init__(self, value):
        self.value = value
        self.edges = []
    def __str__(self):
        return self.value

Just keep in mind that this will happen any time you print and there may be times you want to distinguish 2 nodes with the same value apart from each other.

CodePudding user response:

Turning @jasonharper's comment into an answer so everyone can easily reference.

The issue was in adding edge.node_from and edge.node_to to the list. Those are references to node objects, so they return the memory location. We need to add edge.node_from.value and edge.node_to.value to get the values from those nodes rather than the memory objects. Thus the method is resolved as:

def get_edge_list(self):
        """Don't return a list of edge objects!
        Return a list of triples that looks like this:
        (Edge Value, From Node Value, To Node Value)"""
        triples = []
        for edge in self.edges:
            triples.append((edge.value, edge.node_from.value, edge.node_to.value))
        return triples

This returns the expected output of:

[(100, 1, 2), (101, 1, 3), (102, 1, 4), (103, 3, 4)]

Thank you for the help everyone!

  • Related