Hi all, first time posting, pls no flame and all that
I’ve made an object tree for a project I’m doing and I’ve noticed some unexpected behavior when adding new children to a parent object. What happens is that when using append() to add an object to an empty children list of a parent, the child gets added to all objects of that class. This means that the child also is its own child, which is bad news for anyone trying to make a tree hierarchy.
The simplest reproducable version I could make was as such:
class Node: children =  a = Node() b = Node() c = Node() a.children.append(b) d = Node() print(a) print(b) print(c) print(d) print(a.children) print(b.children) print(c.children) print(d.children)
With the following output:
<__main__.Node object at 0x7f2941eb3af0> <__main__.Node object at 0x7f2941eb3760> <__main__.Node object at 0x7f2941eb37c0> <__main__.Node object at 0x7f2941eb3d00> [<__main__.Node object at 0x7f2941eb3760>] [<__main__.Node object at 0x7f2941eb3760>] [<__main__.Node object at 0x7f2941eb3760>] [<__main__.Node object at 0x7f2941eb3760>]
As can be seen, Node b is now a child of every Node created, even Nodes created after appending.
My question is: is this intended behavior? If so, for what purposes? Because this doesn’t happen when appending to lists with length >= 1, and one can use a.children = [b] for the empty list case to only add to a.