[Tutor] why can you swap an immutable tuple?

Jim Mooney cybervigilante at gmail.com
Sat May 25 20:56:51 CEST 2013


I thought tuples were immutable but it seems you can swap them, so I'm
confused:

a,b = 5,8

print a  # result 5

a,b = b,a

print a  # result 8, so they swapped

# but if I test the type of a,b I get a tuple

testit = a,b
print type(testit)  #comes out as a tuple

print testit, # result is (8,5)

# So how am I swapping elements of a tuple when a tuple is immutable?

#t rying so change a tuple since I just did it

testit[1] = 14  #program crashes - *TypeError: 'tuple' object does not
support item assignment *so the tuple I just changed is indeed immutable

-- 
Jim Mooney

"Coding? You can get a woman to do that!" he said with a sneer. --heard
from a physics professor in 1969
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130525/bcf045bf/attachment.html>


More information about the Tutor mailing list