Tuples and immutability

Mark H. Harris harrismh777 at gmail.com
Sat Mar 1 01:22:02 CET 2014


On Thursday, February 27, 2014 10:01:39 AM UTC-6, Eric Jacoboni wrote:

> 
> ('spam', [10, 30, 20], 'eggs')
> 

> I get a TypeError for an illegal operation, but this operation is still
> 
> completed?

hi Eric,  others have answered your question specifically, but the issue (which is recurring) begs a couple of questions, which will also be recurring...  ehem.

This little snag (if it can be called that) is a side effect from two python inherent design considerations:

1)  not to be a nudge, but dynamic typing is the first...

2)  and the concept of immutability is the second.

I'll address the second first by asking a question...  should an immutable type (object) be able to hold (contain) mutable objects ... should tuples be allowed to hold lists? 

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...

Which brings me to my second point... dynamic binding...  (and everything associated with that) is at play here too....  please, don't get me wrong,  this is not flame bait and I'm not advocating static binding, nor do I want static binding, nor do I want to open that can of worms again... just saying.

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). 

Soooo....   the type does not support item assignment, yet the item got assigned.  This is at least inconsistent... and to my small mind means... bug report.

:)



More information about the Python-list mailing list