[newbie] A question about lists and strings

Chris Angelico rosuav at gmail.com
Fri Aug 10 12:37:24 CEST 2012


On Fri, Aug 10, 2012 at 8:12 PM, Mok-Kong Shen
<mok-kong.shen at t-online.de> wrote:
> Thanks for the explanation of the output obtained. But this means
> nonetheless that parameters of types lists and strings are dealt with
> in "inherently" (semantically) different ways by Python, right?

It's nothing to do with parameters, but yes, lists are mutable and
strings are immutable. A tuple will behave the same way a string does:

>>> a
(1, 2, 3, 4)
>>> b=a
>>> a+=5,    # note that "5," is a one-element tuple
>>> a
(1, 2, 3, 4, 5)
>>> b
(1, 2, 3, 4)


By the way:

On Fri, Aug 10, 2012 at 8:07 PM, Dave Angel <d at davea.name> wrote:
> But if you said  c=651 and d=651, you'd have two
> objects, and the two names would be bound to different objects, with
> different ids.

To be more accurate, you *may* have two different objects. It's
possible for things to be optimized (eg with small numbers, or with
constants compiled at the same time).

ChrisA


More information about the Python-list mailing list