Class point is defined as (there are also some methods, atributes, and stuff in it, but this is minimal part):
class point():
def ___init___(self, x, y):
You're looking at G[0]
. But that's not what you want. G[0]
contains the information about neighbors of node 0
and the attributes of the edges, but nothing about the attributes of node 0
.
class point():
def __init__(self, x, y):
self.x = x
self.y = y
import networkx as nx
G = nx.Graph()
p0 = point(0,0)
p1 = point(1,1)
G.add_node(0, data=p0)
G.add_node(1, data=p1)
G.add_edge(0,1, weight=4)
G[0]
> AtlasView({1: {'weight': 4}}) #in networkx 1.x this is actually a dict. In 2.x it is an "AtlasView"
For networkx there is an expectation that a node may have a lot of data associated with it. In your case, you only have a single piece of data, namely the point. But you could have also assigned a color, a weight, a time, an age, etc. So networkx is going to store all the attributes in another dictionary, but that dictionary is accessed through G.node[0]
rather than G[0]
.
G.node[0]
> {'data': <__main__.point at 0x11056da20>}
G.node[0]['data'].x
> 0
Notice that data
in your input becomes the string 'data'
.
It might be better to enter the nodes like G.add_node(0, x=0, y=0)
and then you can access the entries as G.node[0]['x']
.