function call questions
chenyong20000 at gmail.com
chenyong20000 at gmail.com
Sun Oct 23 09:27:47 EDT 2016
在 2016年10月22日星期六 UTC+8下午9:15:06,Frank Millman写道:
> wrote in message
> news:9c91a4cf-1f3e-43b3-b75c-afc96b0b406e at googlegroups.com...
>
> > I have read Anssi's post already before I sent the post. To be frankly, I
> can't understand why he got the right answer. I'm sorry for my silly. "So
> when we assign to r again, it's the empty dict inside t (the one accessed
> by key 'a')". I do can't understand why this happens. that is the reason why
> I have asked for this once again and again. There must be some import point
> I missed but I don't what is it.
>
> Let's try this -
>
> >>> t = {}
> >>> r = t
> >>> r = r.setdefault('a', {})
> >>> t
> {'a': {}}
>
> I think you are happy up to this point.
>
> We now have three objects -
>
> "t" is a dictionary
> 'a' is a key in the dictionary
> {} is the value associated with the key 'a' in "t"
>
> I think you are happy up to this point.
>
> The question is, what is "r"?
>
> Before the assignment, "r" was a reference to the dictionary referenced by
> "t".
>
> After the assignment, "r" no longer refers to "t". It is now a reference to
> the
> third object listed above, the {} that is the value associated with the key
> 'a'.
>
> >>> t
> {'a': {}}
> >>> t['a']
> {}
> >>> r
> {}
> >>> t['a] is r
> True
>
> Keep looking at this until it sinks in. "r" and "t['a']" are *the same
> object*. We just have two ways of accessing it.
>
> Try adding some key/values to the empty dictionary -
>
> >>> r['x'] = 99
> >>> r
> {'x': 99}
> >>> t['a']
> {'x': 99}
> >>> t
> {'a': {'x': 99}}
>
> I will pause at this point, and give you a moment to absorb that.
>
> Hopefully, the penny will drop and everything will become clear.
>
> If not, let us know which of the above steps you do not understand.
>
> Good luck - keep plugging away, and you will get there :-)
>
> Frank
>
> P.S. I assume you understand that the lines prefixed with '>>>' are to be
> entered while in the python interpreter. It is really important that you
> type these lines in yourself and examine the results.
Hi Frank,
I got it this time. Thanks very much for your help. I thought r is an empty dictionary without any connection to t before. Now I know that happened. Thanks.
regards
skyworld
More information about the Python-list
mailing list