function call questions

chenyong20000 at gmail.com chenyong20000 at gmail.com
Tue Oct 18 05:28:26 EDT 2016


> > My question is:
> > (1) why root is always {}?
> 
> Because that's what you bind it to in the line
> 
> > ...      root = root.setdefault(ch,{})
> 
> See <https://docs.python.org/dev/library/stdtypes.html#dict.setdefault>
> for a description of the the setdefault() method.

I'm still confused here. root.setdefault(ch,{}) should return {'a':{}}, right? then this dictionary is referenced by root by "root = root.setdefault(ch,{})". why root is an empty dictionary now?


> 
> > (2) why tree is {'a': {'b': {'c': {}}}}?
> 
> Basically the same answer -- you bind root to the innermost dict and that's 
> where you insert the ch key on the next iteration of the for loop
> .
> > (3) why root isn't the same as tree? shouldn't they be the same because
> > tree is argument passed as root?
> 
> You should know the answer by now ;)

In fact i'm still confused by these answers. Could you please described more? thanks




More information about the Python-list mailing list