简体   繁体   中英

Networkx AttributeError: 'DiGraph' object has no attribute '_succ' on inheritence

Why does the following code

import networkx as nx

class DiGraph(nx.DiGraph):
    def __init__(self):
        self.add_node("a")
        super().add_node('a')

d  = DiGraph()

produce an attribute error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_9038/3565896072.py in <module>
      6         super().add_node('a')
      7 
----> 8 d  = DiGraph()

/tmp/ipykernel_9038/3565896072.py in __init__(self)
      3 class DiGraph(nx.DiGraph):
      4     def __init__(self):
----> 5         self.add_node("a")
      6         super().add_node('a')
      7 

/usr/lib/python3/dist-packages/networkx/classes/digraph.py in add_node(self, node_for_adding, **attr)
    418         doesn't change on mutables.
    419         """
--> 420         if node_for_adding not in self._succ:
    421             self._succ[node_for_adding] = self.adjlist_inner_dict_factory()
    422             self._pred[node_for_adding] = self.adjlist_inner_dict_factory()

AttributeError: 'DiGraph' object has no attribute '_succ'

My.networkx version is 2.4

import networkx as nx

class CusDiGraph(nx.DiGraph):
    def __init__(self):
        super(CusDiGraph, self).__init__()
        self.add_node("a")
        super().add_node('a')

d = CusDiGraph()


# plt
import matplotlib.pyplot as plt
pos=nx.spring_layout(d) # pos = nx.nx_agraph.graphviz_layout(G)
nx.draw_networkx(d,pos)
labels = nx.get_edge_attributes(d,'weight')
nx.draw_networkx_edge_labels(d,pos,edge_labels=labels)

Just change the inheritance relationship.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM