python dictionary (copy by value or by reference)

trying to understand why this is true

step 1: x = {}
step 2: y = x
step 3: x['key'] = 'value'
# at this point if i print x or y i see {'key', 'value'}
step 4:  x['key'] = 'newValue' #and at this point printing x or y i see {'key', 'newValue'} and this is true if this was y['key']

because of the behavior in step 4, i'm thinking, ok the copy job from step 2 was a pointer only and not a by value copy job. fair enough

step 5: x = {} #or y={}
step 6: print both. and what i get here is that x will be empty but y will not (or visa verse)

question: if y=x from step 2 (the copy job)  is just creating a pointer y that points to the same thing as x then why when i set x = {} in step 5 does that also not cause y to equal {}?

what am i not understanding about python dictionaries?
alexmac05Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alexmac05Author Commented:
i know this is why we're supposed to use x.clear() but what i'm looking for is why this behavior exists so i can understand
0
peprCommented:
Any Python assignment means binding the variable to the assigned target object via its address. (Like one-way reference. You are looking at the identical object through or x or y names.)

Using {} means creating a new dictionary object.

Python variables are implemented as items in an internal dictionary where identifiers are strings used as keys, values are addresses of the target objects. The assignment y=x means creating another item in the internal dictionary -- the value part (the address) is copied.

Try also the built-in function id() that returns the address, or the operator 'is' that compares the addresses (like print(id(x)), the same for y, and then print(x is y) ).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.