[Tutor] why can you swap an immutable tuple?

Steve Willoughby steve at alchemy.com
Sat May 25 21:29:48 CEST 2013


On 2013-5月-25, at 上午11:56, Jim Mooney wrote:

> I thought tuples were immutable but it seems you can swap them, so I'm confused:
> 
> a,b = 5,8
> 


I think you're confusing mutating the tuples with assigning the immutable tuples different names.  The variable names are just labels you attach to them.  The tuple values themselves cannot change.  

That said, your example here isn't a tuple but a pair of singleton values. 

If you said 
tuple1 = 1,2,3,4
tuple2 = 5,6,7,8

you create two tuples which are, yes, immutable.  They happen to be called tuple1 and tuple2 but that's beside the point of immutability.  If you pass them to a function, they'll be known locally there under different names but they're still immutable tuples.

Does that help?
--steve


More information about the Tutor mailing list