just registered at stackoveflow because of a weird class attribute change problem I encountered in python 3.6
(editor: spyder 3.1.4
). Not sure how to describe it appropriately so my apology if similar problems have been asked before. Following is the problematic code snippet:
class Node:
def __init__(self, s = '', c = []):
self.s = s
self.c = c
...
def somefunction(p):
currentnode = a_node # a previous Node instance
p_node = Node(s = p) # debugging showed after this step, p_node.c == []
currentnode.c.append(p_node) # now, somehow p_node.c == [ p_node ], and still currentnode is not p_node
currentnode = p_node
I have checked to make sure no accidental mutual references, and have changed the attribute Node.c
from list()
to set()
, same thing happened. Any explanation on this accidental class attribute value change? Thanks a bunch!