about sort and dictionary

bonono at gmail.com bonono at gmail.com
Tue Nov 22 10:30:18 CET 2005

Magnus Lycka wrote:
> sorted_l = l.sort()
> and while sorted_l would contain what one might expect, it
> would in fact just be another name referencing exactly the
> same sorted list as l, and it would probably be surprising
> that l was also sorted, and that subsequent changes would
> show up in both sorted_l and l, and that sorted_l might not
> be sorted and longer even though you only modified l. It's
> this particular gotcha that the language creator wanted to
> avoid.
Since python's '=' is just name binding and that most objects(other
than those like int/float/string?) are mutable, I don't quite
understand why this is a gotcha that is so worrying.

a = [1,2,3]
b  = a

even an entry level python programmer can't expect 'b' to be
unchanged(after getting the first bite may be) if there is any
operation on a later. This not only applies to list but almost all
mutable object.

As you said, if one wants a copy of an object, use copy/deepcopy or
even pickle to get a snapshot of it.

More information about the Python-list mailing list