wrote in message
news:9c91a4cf-1f3e-43b3-b75c-afc96b0b4...@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.


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

Reply via email to