Home > front end >  Python, List of Children
Python, List of Children

Time:12-09

I want to generate a parent/child datastructure in Python 3.8.10. [GCC 9.3.0] on linux The code is as simple as:

class Member(object):
    name = "xxx"
    parent = None
    children = []
    
    def __init__(self, name):
        object.__init__(self)
        self.name = name
    
    def setParent(self,p):
        self.parent = p
        
    def addChild(self,ch):
        self.children.append(ch)
        ch.setParent(self)
        
    def myRelatives(self):
        str = "I am {0}, my parent is {1} my children are:".format(self.name,self.parent.name if self.parent is not None else "--")
        print(str)

        for ch in self.children:
            print("   {0}".format(ch.name))
       

if __name__ == '__main__':
    A = Member("A")
    B = Member("B")
    C = Member("C")
    D = Member("D")
    
    A.addChild(B)
    B.addChild(C)
    C.addChild(D)
    
    A.myRelatives()
    B.myRelatives()
    C.myRelatives()
    D.myRelatives()

I have expected:

I am A, my parent is -- my children are:
   B
I am B, my parent is A my children are:
   C
I am C, my parent is B my children are:
   D
I am D, my parent is C my children are:

However my output is:

I am A, my parent is -- my children are:
   B
   C
   D
I am B, my parent is A my children are:
   B
   C
   D
I am C, my parent is B my children are:
   B
   C
   D
I am D, my parent is C my children are:
   B
   C
   D

It seems the 'self.children' is used as same variable in all instances. Why and how to fix?

Thanks, Bernd

CodePudding user response:

You need to set the attributes name, parent, and children in the __init__ function, so e.g. like so:

    def __init__(self, name):
        super().__init__(self)
        self.name = name
        self.parent = None
        self.children = list()

Then it should work.

EDIT: Added improvement suggested by @chepner.

  • Related