在 2016年10月20日星期四 UTC+8下午11:04:38,Frank Millman写道:
> wrote in message 
> news:01cfd810-0561-40b1-a834-95a73dad6...@googlegroups.com...
> 
> 在 2016年10月20日星期四 UTC+8下午1:32:18,Frank Millman写道:
> > wrote in message
> > news:5506e4d8-bd1d-4e56-8d1b-f71fa8293...@googlegroups.com...
> >
> > > Let's see if I can explain. I am using 't' and 'r' instead of 'tree' and
> > > 'root', but otherwise it is the same as your original example.
> > >
> > > >>> t = {}
> > > >>> r = t
> > > >>> id(t)
> > > 2542235910088
> > > >>> id(r)
> > > 2542235910088
> > >
> > > At this point, t and r are both references to the same empty dictionary.
> > >
> > > >>> r = r.setdefault('a', {})
> > >
> > > This has done two things.
> > >
> > > It has inserted the key 'a' into the dictionary, and set its value to 
> > > {}.
> > >
> > > >>> t
> > > {'a': {}}
> > > >>> id(t)
> > > 2542235910088
> > >
> > > It has also rebound 'r' so that it now references the new empty 
> > > dictionary
> > > that has been inserted.
> > >
> > > >>> r
> > > {}
> > > >>> id(r)
> > > 2542234429896
> > > >>>t['a']
> > > {}
> > > >>> id(t['a'])
> > > 2542234429896
> > >
> > > Now continue this process with r = r.setdefault('b', {}), and watch what 
> > > happens.
> >
> > thanks very much for your kind help. Your reply is clear. But I'm hindered 
> > by those you've not explained.
> >
> > I'm always confused by "r = r.setdefault('a', {})". when the first loop 
> > finished, as what you have pointed out,
> > > >>> t
> > > {'a': {}}
> > > >>> r
> > > {}
> >
> > Then next "r = r.setdefault('b', {})" will run again. Here what is "r" in 
> > "r.setdefault('b',{})"? According to final result, it should be "t['a']", 
> > which I can't understand. I thought the command is r.setdefault, so it 
> > should still be last "r", i.e., empty {}. Could you please let me know 
> > what I missed? thanks.
> 
> Firstly, I want to explain more clearly what I am doing here.
> 
> Instead of running your loop 3 times, I am running your command three times 
> one step at a time (though I only showed the first one).
> 
> >>> t = {}
> >>> r = t
> >>> r = r.setdefault('a', {})
> >>> r = r.setdefault('b', {})
> >>> r = r.setdefault('c', {})
> 
> This should give exactly the same result as your loop. The benefit of 
> running it this way is that you can check the values after each step.
> 
> May I suggest that you do this, and try to understand the contents of 't' 
> and 'r' at each point. If you are still unsure, let us know at which point 
> the values are not what you expect, and I will try to explain further.
> 
> It is important that you understand that you are rebinding 'r' at each step, 
> so after each command, 'r' is no  longer referencing the same object that it 
> was referencing in the previous step.
> 
> To see the difference, try running it it this way -
> 
> >>> t = {}
> >>> r = t
> >>> r.setdefault('a', {})
> >>> r.setdefault('b', {})
> >>> r.setdefault('c', {})
> 
> Hope this helps.
> 
> Frank
Hi Frank,

thanks for your kind help. What confused me is at this line:

> >>> r = r.setdefault('b', {})

and its previous one

> >>> r = r.setdefault('a', {})

When r.setdefault('a',{}) is run, I understand it will return an empty {}. At 
this time both r & t reference to {'a':{}}, right? So when "r = 
r.setdefault('a',{})" is run, r reference to {} while t keeps the same as 
{'a':{}}.

then comes r.setdefault('b',{}). What hinder me is here. since r has changed 
its reference to {}, r.setdefault('b',{}) will return {} again. So what does 
this done to t? why at this time t changes to {'a':'b':{}}? Sorry for my silly 
here. Thanks


-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to