class - Odd behaviour with simple tree implementation in python -
class node: children = {} sequence = [1,2,3,4,5] tree = node() node = tree item in sequence: if item not in node.children: node.children[item] = node() node = node.children[item] print tree.children.keys()
i want above code output [1]
, outputs [1, 2, 3, 4, 5]
. why this, , how go fixing it?
node.children
class attribute. make instance attribute instead.
class node: def __init__(self): self.children = {}
Comments
Post a Comment