Skip to content

Change __repr__ for AGraph? (migrated from Trac #341) #338

Closed

Description

@networkx-trac

Original ticket https://networkx.lanl.gov/trac/ticket/341
Reported 2010-04-16 by @chebee7i, assigned to @hagberg.

In NetworkX, the repr for graphs is just the memory address:

'''
In [1]: x = nx.Graph()

In [2]: x
Out[2]: <networkx.classes.graph.Graph object at 0x904e34c>
'''

In PyGraphviz, repr gives the dot representation instead.
'''
In [3]: x = pygraphviz.AGraph()

In [4]: x
Out[4]:
strict graph {
}
'''

If any function, such as like networkx.view_pygraphviz(), returns an AGraph instance, the output in IPython is a bit ugly.
'''
In [5]: x = nx.path_graph(3)

In [6]: nx.view_pygraphviz(x)
Out[6]:
('/tmp/tmpoQmSzp.png',
strict graph "path_graph(3)" {
node [fillcolor="#0000FF40",
height="0.75",
shape=circle,
style=filled,
width="0.75"
];
edge [fontsize=10];
0 -- 1;
1 -- 2;
})
'''

If the behavior were more consistent with NetworkX we'd see:
'''
In [5]: x = nx.path_graph(3)

In [6]: nx.view_pygraphviz(x)
Out[6]:
('/tmp/tmpoQmSzp.png', <pygraphviz.agraph.AGraph object at 0x904e34c>)
'''

I claim we don't lose much since a user can always do:
'''

print agraph.to_string()
'''

So should repr be removed (defaulting to the default Python repr)?

Activity

  1. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2010-04-16

    The general guidance is to make repr make an
    unambiguous representation that in a perfect world
    could be passed to eval().

    See e.g

    http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python

    So maybe it is more likely that the NetworkX class repr's are lacking.

  2. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2010-07-23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions