function call questions

Anssi Saari as at sci.fi
Tue Oct 18 05:13:46 EDT 2016


chenyong20000 at gmail.com writes:

> My question is:
> (1) why root is always {}?

Because that's what you wrote. root.setdefault(ch, {}) returns {} and
you assign that to root. You probably want to do just
root.setdefault(ch, {}) instead of root = root.setdefault(ch, {}).

> (2) why tree is {'a': {'b': {'c': {}}}}?

That I don't know. Seems odd to me. From some debugging it seems that
after root = root.setdefault(ch,{}) tree['a'] and root are the same
object. So on the second round your code calls
tree['a'].setdefault('b',{}) and on the third round
tree['a'].setdefault('c',{}) which results in {'a': {'b': {'c': {}}}} as
the value of tree.

> (3) why root isn't the same as tree? shouldn't they be the same because tree is argument passed as root?

They are the same until you bind root to a new object with root =
root.setdefault(ch,{}).



More information about the Python-list mailing list