Alex McFerron <pythonistaforh...@gmail.com> writes: > trying to understand why this is true > > step 1: x = {}
Assignment; binds a reference (the name ‘x’) to a newly-created empty dictionary. > step 2: y = x Assignment; binds a reference (the name ‘y’) to the object currently referred to by ‘x’. That's the same object as above. > step 3: x['key'] = 'value' Assignment; binds a reference (the item keyed by the string ‘'key'’ in the same dictionary as above) to the string object ‘'value'’. > # at this point if i print x or y i see {'key', 'value'} Because there's only one dictionary in all of this. > step 4: x['key'] = 'newValue' Assignment; binds a reference, the same reference as before (the item keyed by the string ‘'key'’ in the same dictionary as above) to the string object ‘'newValue'’. > #and at this point printing x or y i see {'key', 'newValue'} and this > is true if this was y['key'] Because there's only one dictionary in all of this. > 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 None of these operations are copies. Assignment in Python is *never* a copy operation. > step 5: x = {} Assignment; binds a reference (the name ‘x’) to a newly created empty dictionary. > step 6: print both. and what i get here is that x will be empty but y will > not (or visa verse) Because the name ‘y’ is still a reference to the original dictionary above. > question: if y=x from step 2 (the copy job) There's your error. Assignment in Python *never* copies, it only binds a reference to some value. -- \ “If you continue running Windows, your system may become | `\ unstable.” —Microsoft, Windows 95 bluescreen error message | _o__) | Ben Finney _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor