On 25 July 2011 17:17, naheed arafat <naheed...@gmail.com> wrote: > I got a question in this context. > suppose > a={'a': 3, 'b': [1, 2], 5: 100} > ------------------b=a -------------- vs---------- > b=copy.copy(a)------------ > ---------------------------------------------------- > b[5]=6 ---------------------------------------- b[5]=6 > output: ----------------------------------------- output: > b={'a': 3, 'b': [1, 2], 5: 6}------------------- b={'a': 3, 'b': [1, 2], > 5: 6} > a={'a': 3, 'b': [1, 2], 5: 6} ------------------- a={'a': 3, 'b': [1, 2], > 5: 100} > that means b=a & b=copy.copy(a) aren't the same. > but > b['b'].append(3) > output: > b={'a': 3, 'b': [1, 2, 3], 5: 100}--------------b={'a': 3, 'b': [1, 2, 3], > 5: 100} > a={'a': 3, 'b': [1, 2, 3], 5: 100}--------------a={'a': 3, 'b': [1, 2, 3], > 5: 100} > now doesn't it mean that b=a & b=copy.copy(a) both are same?
No, b=a makes a new reference to the _same_ dict object. b=copy.copy(a) creates a new dict object b. However it does not make a copy of the *contents* of the dict object. To make a copy of the contents of the dict use cop.deepcopy(). Play around with id() on the a, b and their contents. But do note that cpython caches small integers so the integer 3 will have the same id (thus it is the same object). greets Sander _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor