[newbie] A question about lists and strings

Dave Angel d at davea.name
Fri Aug 10 06:56:12 EDT 2012


On 08/10/2012 06:37 AM, Chris Angelico wrote:
> 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

You're right, of course.  But I picked the value of 650+ deliberately,
as I believe CPython doesn't currently optimize ints over 256.



-- 

DaveA




More information about the Python-list mailing list