0

I have the this following class that specifies how to create a node. A node object has a name, a parent and a list of children.

I have one method to add children which works. Calling the add_child method adds a child to the list of children. I first create a node with name 'A' and then add a child to A which has the name 'B'. One can check that a node with name attribute 'B' is then in the list of A's children. However I am unable to access this node object created (execept by calling A.children[0])

class Node:
    

    def __init__(self,name,parent=None,children=[]):
        #init defines the instance variables with . notation
        self.name=name
        self.children=children
        self.parent=parent
        
    def __str__(self):
        return str('node'+' '+self.name)    
               
        
    
    def get_children(self):
        
        children=self.children
        names=[str(c) for c in children]
        print(names)
        return(children)
        
        
    def add_child(self,child):
        newchild=Node(child,self,children=[])
        self.children.append(newchild)
        

A=Node('A',None,[])
A.add_child('B')

Is there any way for me to specify the reference to the new child object that has been created so that later I can access it easily withouth having to refer to the list of A's children?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
kangkan Dc
  • 183
  • 1
  • 9
  • What's the point of the class if you don't want to use it? – OneCricketeer Jul 11 '22 at 17:54
  • Also, what if you have `A=Node('A',None,[]); C=Node('C',None,[]); A.add_child('B'); C.add_child('B')`... Then, which "B" do you want? – OneCricketeer Jul 11 '22 at 17:56
  • Once `add_child` returns, the *only* reference to the `Node` object is held by `self.children`. If you want the reference outside the method, return it. – chepner Jul 11 '22 at 18:01
  • 1
    Unrelated, don't use `[]` as the default argument value for `children` in `Node.__init__`. See https://stackoverflow.com/q/1132941/1126841 – chepner Jul 11 '22 at 18:02

1 Answers1

0

I think I figured out a solution. I use a dictionary to store the children in which I use the node's name as the object key. However, I do think just adding a return to get the add_child method is more direct.

class Node:
    def __init__(self,name,parent=None,children={}):
        #init defines the instance variables with . notation
        self.name=name
        self.children=children
        self.parent=parent
        
    def __str__(self):
        return str('node'+' '+self.name)    
               
        
    def get_parent(self):#define a method
        
            return(str(self.parent))
    def get_children(self):
        
        children=self.children
        names=[str(c) for c in children.keys()]
        print(names)
        return(children)
        
        
    def add_child(self,childname):
        newchild=Node(childname,self,children={})
        self.children[childname]=newchild
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
kangkan Dc
  • 183
  • 1
  • 9