-1

When I run this code:

class Node:
    children = []
    num_children = 0
    isleaf = True
    def __init__(self,name):
        print("constructor called for " + name)
        self.name = str(name)



    def add_child(self,child_node):
        print("adding child called "+child_node.name+ " to "+ self.name)
        print("I ("+self.name+ ") have " + str(len(self.children)) + " children")
        print("checking: " + child_node.name + " reports " + str(len(child_node.children)) + " children")
        #cc = copy.deepcopy(child_node)
        self.children.append(child_node)
        #self.children.append(1)
        #self.children[0] = child_node
        print("checking again: " + child_node.name + " reports " + str(len(child_node.children)) + " children")
        self.isleaf = False
        self.num_children = self.num_children +1
        print("I ("+self.name+ ") now have " + str(len(child_node.children)) + " children")



root = Node("a")


testn = Node("b")

root.add_child(testn)


print(root.children[0].name)

I get this output:

constructor called for a
constructor called for b
adding child called b to a
I (a) have 0 children
checking: b reports 0 children
checking again: b reports 1 children
I (a) now have 1 children
b

why does the line of code:

self.children.append(child_node)

add a child to the child (B) as well as the parent (A)?

I would expect it to only add a child to the parent (A)

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
RNs_Ghost
  • 1,687
  • 5
  • 25
  • 39
  • Do you want `children` to be a class variable? –  Nov 04 '16 at 11:35
  • yes children should be a member of node. I'm trying and failing to implement a tree – RNs_Ghost Nov 04 '16 at 11:36
  • If you want `children` to be an instance variable, make it one. See my answer below. –  Nov 04 '16 at 11:37
  • @jonrsharpe this is not a duplicate as the error was caused by a typo. Why did you downvote it? – RNs_Ghost Nov 04 '16 at 11:48
  • Apparently you had more than one problem. The issue with data being shared amongst instances, the original question you asked, is definitely the duplicate I closed this against, as the answers that were posted tell you. If there's another issue, that should be a separate question, don't edit the question with your next problem. If the new problem is just a typo, don't ask it at all; they are not useful to others. And please don't edit answers into the question, either. – jonrsharpe Nov 04 '16 at 11:56
  • @jonrsharpe If you don't know the nature/cause of the error then its impossible to know if another question contains the answer. Hence down voting serves no useful purpose – RNs_Ghost Nov 04 '16 at 14:04
  • But we do know the cause of the error. As reported, as you wrote the question, data was being shared unexpectedly because you have a mutable class attribute when you needed an instance attribute. If that's not actually the problem you're trying to solve that's on you, as the author of the question; the rest of us can only go on what you write. If you have another problem subsequent to solving the first one, that should be the subject of a separate inquiry (unless, again, it's just a typo). – jonrsharpe Nov 04 '16 at 14:05

2 Answers2

0

Move children = [] into __init__ to make it an instance member. Right now it is a class member:

def __init__(self, name):
    self.children = []
    self.num_children = 0
    self.isleaf = True
0

That's because you defined children as a class variable and not as an instance variable. Meaning that children doesn't really belong to root or testn but rather to the class Node itself.

To fix it just replace children = [] by self.children = [] inside the __init__.

jadsq
  • 3,033
  • 3
  • 20
  • 32