function call questions

chenyong20000 at gmail.com chenyong20000 at gmail.com
Tue Oct 18 03:36:15 EDT 2016


Hi,
I got a function call as this:

>>>def add_to_tree(root,value_string):
...    print "root is %s, value_string is %s" % (root, value_string)
...    for ch in value_string:
...      print "ch is %s" % ch
...      root = root.setdefault(ch,{})
...      print "root is", root
...      print "tree is", tree
...
>>> tree={}
>>> txt='abc'
>>> add_to_tree(tree,txt)
root is {}, value_string is abc
ch is a
root is {}
tree is {'a': {}}
ch is b
root is {}
tree is {'a': {'b': {}}}
ch is c
root is {}
tree is {'a': {'b': {'c': {}}}}

My question is:
(1) why root is always {}?
(2) why tree is {'a': {'b': {'c': {}}}}?
(3) why root isn't the same as tree? shouldn't they be the same because tree is argument passed as root?


regards
skyworld



More information about the Python-list mailing list