I wrote following code:
class node:
def __init__(self, title, series, parent):
self.series = series
self.title = title
self.checklist = []
if(parent != None):
self.checklist = parent.checklist
self.checklist.append(self)
When I create objects like this:
a = node("", s, None)
b = node("", s, a)
print a.checklist
Unexpectedly, it shows both a and b objects as an output of print statement.
I am new to python. So, possibly there’s some stupid mistake.
Thank you.
You do
self.checklist = parent.checklistwhich means that both instances share the same list. They both add themselves to it, so when you print it you see both instances.Maybe you wanted to make a copy of the parent list?
self.checklist = parent.checklist[:]