about sort and dictionary
Fredrik Lundh
fredrik at pythonware.com
Tue Nov 22 04:55:52 EST 2005
bonono at gmail.com wrote:
> 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]
> a.sorted()
> 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.
so what would an entry-level Python programmer expect from this
piece of code?
for item in a.reverse():
print item
for item in a.reverse():
print item
(as the X people used to say, the only thing worse than generalizing
from one example (sort) is generalizing from no examples at all ("let's
assume I have a use case")).
</F>
More information about the Python-list
mailing list