Mutable objects inside tuples - good or bad?

Devin Jeanpierre jeanpierreda at gmail.com
Sun Apr 6 07:01:10 EDT 2014


On Sun, Apr 6, 2014 at 1:25 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Sun, Apr 6, 2014 at 5:55 PM, Devin Jeanpierre <jeanpierreda at gmail.com> wrote:
>> Agreed. Putting mutable objects inside tuples is common and totally OK.
>
> There are many programming habits that can cause problems, even though
> they won't break Python and are legal code. :)

Yes, but this isn't one of them.

>> c is such that you could have another variable d, where the following
>> interpreter session fragment is easily possible:
>>
>>>>> c == d
>> True
>>>>> foo(c)
>>>>> c == d
>> False
>
> What you're looking at here is hashability, not mutability. Compare:

No, that is not what I am looking at. There are hashable objects with
the property I described, and unhashable objects without it.

My point in that example was that sometimes it is more useful to talk
about entire objects and their behavior as a whole. Calling the object
"immutable" when it has mutable state is misleading in this context.

-- Devin



More information about the Python-list mailing list