Mutable objects inside tuples - good or bad?

Paul Kölle paul at subsignal.org
Mon Apr 7 15:46:37 EDT 2014


Am 07.04.2014 17:44, schrieb Chris Angelico:
> On Tue, Apr 8, 2014 at 1:26 AM, Paul Kölle <pkoelle at gmail.com> wrote:
>> It seems a tuple's immutability is debatable, or is this another instance of
>> the small-integer-reuse-implementation-detail-artifact?
>>
>> Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
>> [GCC 4.4.5] on linux2
>>
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> a = ([1,2],[3,4])
>>>>> b = a
>>>>> a is b
>> True
>>>>> a == b
>> True
>>>>> c = (1,2,3)
>>>>> d = (1,2,3)
>>>>> c is d
>> False
>>>>> c == d
>> True
>
> That's nothing to do with mutability or reuse. With a and b, you
> assigned one to be the same as the other, so they are by definition
> identical (and equal; tuples assume that identity implies equality,
> even though that may not be true of their elements). With c and d, you
> assigned separate tuples, so they're allowed to be separate objects.
> I'm not sure if they're allowed to be constant-folded, but CPython
> apparently isn't doing so. They are still equal, though; they contain
> equal elements, ergo they are equal. (Note that (1, 2, 3) and (1.0,
> 2.0, 3.0) are equal, but they obviously can't be identical any more
> than "1 is 1.0" can ever be True.)
>
> ChrisA
>
Thanks Chris, stupid error indeed ;)

cheers
  Paul





More information about the Python-list mailing list