how are strings immutable in python?

Peter Otten __peter__ at web.de
Sun Jul 6 14:57:00 EDT 2008


ssecorp wrote:

>>>> h = "aja baja"
>>>> h += 'e'
>>>> h
> 'aja bajae'
>>>>

The inplace-add operator doesn't mutate the lvalue, it just rebinds it:

>>> a = b = "foo"
>>> id(a)
47643036142016
>>> a += "bar"
>>> id(a), a
(47643036142064, 'foobar')
>>> id(b), b
(47643036142016, 'foo')

Peter



More information about the Python-list mailing list