Tuples and immutability
rosuav at gmail.com
Sat Mar 1 02:39:05 CET 2014
On Sat, Mar 1, 2014 at 11:22 AM, Mark H. Harris <harrismh777 at gmail.com> wrote:
> lists within a tuple should be converted to tuples. If you want a tuple to hold a list, make it a list in the first place. Tuples should not be changed... and as you point out... half changing a tuple is not a good condition if there is an exception...
A tuple is perfectly fine containing a list. If you want a tuple to be
"recursively immutable", then you're talking about hashability, and
*then* yes, you need to convert everything into tuples - but a tuple
is not just an immutable list. The two are quite different in pupose.
> I really think this is a bug; honestly. IMHO it should be an error to use += with an immutable type and that means not at all. In other words, the list should not even be considered, because we're talking about changing a tuple... which should not be changed (nor should its members be changed).
Definitely not! Incrementing an integer with += is a perfectly normal
thing to do:
x = 5
x += 1
It's just a matter of knowing the language and understanding what's going on.
More information about the Python-list