error is raised when I try to remove elements (pydot objects) from a python list
Question: I am writing an algorithm to represent regression trees, using pydot module
(an interface to Graphviz's Dot language). In the algorithm, lists of Edges
and Nodes are made, and then they are represented - that is working fine.

But in some specific situations, I need to remove some of the Edges and Nodes,
and that's where I am getting stuck. Here is part of the code:

    
    
    import pydot
    graph = pydot.Dot(graph_type='graph')
    
    link4 = pydot.Edge(node10, node21, label=etiquetas[3])
    link5 = pydot.Edge(node11, node22, label=etiquetas[4])
    lista_links = [link4, link5]
    
    # if some conditions are verified, then:
    lista_links.remove(link5)
    
    for link in lista_links:
    graph.add_edge(link)
    graph.write_png('teste.png')
    

I was expecting this code to work without any problem, but I get an error,
saying:

    
    
    AttributeError: 'NoneType' object has no attribute 'get_top_graph_type'
    

My only idea is, instead of removing the Nodes and Edges in some specific
situations, to change the code and add only the Nodes and Edges after I define
all the specific situations. But that would be a lot more work... (The code is
much bigger than what I've shown you, and I have several specific situations
that need to be considered).

I am curious why python behaves like this... Can somebody explain that to me,
or give me any idea on how to change this behavior?

Thanks in advance, Carla


Answer: At the surface, it seems that the problem is in Edges or Nodes without parent
graph. So, the overall solution would be: do not allow nodes and edges hang
around, always attach them to graph,and then remove from graph as needed.

