mutate dictionary or list
ben+python at benfinney.id.au
Tue Sep 7 16:46:43 CEST 2010
deets at web.de writes:
> Objects can be mutable or immutable. For example, in Python, integers,
> strings, floats and tuples are immutable. That means that you can't
> change their value.
Yes. Importantly, wherever you see code that you *think* is changing the
value of an immutable object, you're thinking incorrectly. (There's no
shame in that; other languages give us preconceptions that can be hard
to shake off.)
The only way to get a different value from an integer object is to ask
Python for a different integer object; the original is unchanged. The
same goes for tuples, strings, and all the other immutable types.
> Mutable objects OTOH can be changed.
Some good articles to explain Python's object model:
\ “We can't depend for the long run on distinguishing one |
`\ bitstream from another in order to figure out which rules |
_o__) apply.” —Eben Moglen, _Anarchism Triumphant_, 1999 |
More information about the Python-list