Given a directed networkx graph. I would like to have a function, say "abc()
", called for every node that is deleted from the graph. I went through the networkx documentation but did not find any such callback feature.
What I considered:
- Adding a call to
abc()
to the__del__()
(deconstructor) method of the object associated with a node. Yet, besides the pitfalls of using__del__()
(see e.g. here) this does not work if any links to the node object exist somewhere in memory. - Subclassing the
networkx.DiGraph()
class and overriding the remove_node() method. Drawback: This would require overriding all methods that remove a node, e.g.remove_nodes_from
(are there any more?) - As the networkx graph implementation is based on dictionaries, it could be a solution to somehow 'hook' the
del
function of this dictionary. Interfering this deep into networkx seems inappropriate though.
What is the easiest way to implement a callback function which is called everytime a networkx node is deleted?