簡體   English   中英

Networkx AttributeError: 'DiGraph' object 在繼承上沒有屬性 '_succ'

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

為什么會出現下面的代碼

import networkx as nx

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

d  = DiGraph()

產生屬性錯誤

---------------------------------------------------------------------------
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 版本是 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)

只需更改 inheritance 關系即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM