Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python, List of Children

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading