Search code examples
pythoninheritancesuperclassnetworkx

networkx: 'super' object has no attribute 'node'


I try extend a class from networkx.DiGraph

import networkx as nx
class branch(nx.DiGraph):
    def __init__(self,g,raiz):
        self.b=super(branch,self)
        self.b.__init__(g)
        self.r = raiz
    def strong(self):
        print self.b.nodes(),self.b.node[self.r]
        if self.b.node[self.r]['w']>0:                                                                                  
            return 1
        else:
            return 0

If I execute I get

[1, 'r']
...
AttributeError: 'super' object has no attribute 'node'

I can use .nodes() but no .node[] , why ?


Solution

  • Simply enough, because node[] doesn't exist. Instead, nodes() returns an array that you can index with []. The code for this might look something like self.b.nodes()[self.r].