I'm new to python. Could someone please explain the following behaviour of a recursive data structure?
def new_node(id='', daughters=[]): return dict(id=id, daughters=daughters) n0 = new_node(id='n0') n1 = new_node(id='n1') ## Seems OK so far: n0 # {'id': 'n0', 'daughters': []} n1 # {'id': 'n1', 'daughters': []} ## Now try to make n1 a daughter of n0: n0['daughters'].append(n1) ## But that seems to have made something funny happen to n1: n1 # {'id': 'n1', 'daughters': [{...}]} ## In fact, n1 seems to have become its own daughter n1['daughters'][0] # {'id': 'n1', 'daughters': [{...}]} ## and grand-daughter, etc etc n1['daughters'][0]['daughters'][0] # {'id': 'n1', 'daughters': [{...}]} ## These changes to n1 are present in n0 (as I would expect) n0 # {'id': 'n0', 'daughters': [{'id':'n1', 'daughters': [...]}]} n0['daughters'][0]['daughters'][0] # {'id': 'n1', 'daughters': [...]} Why did the append() operation have this effect? Straight assignment seems to do what I expected, i.e. n0['daughters'] = [n1] n1 # still the same {'id': 'n1', 'daughters': []} n0 # {'id': 'n0', 'daughters': [{'id': 'n1', 'daughters': []}]} Thanks for any enlightenment, Dan ~> python --version Python 2.5.2 ~> uname -a Linux Tichodroma 2.6.27-11-generic #1 SMP Thu Jan 29 19:24:39 UTC 2009 i686 GNU/Linux Ubuntu 8.10 -- http://mail.python.org/mailman/listinfo/python-list