# [Tutor] Tuple - Immutable ?

12 messages
Open this post in threaded view
|

## [Tutor] Tuple - Immutable ?

 >>> s=(1,2,3)>>> s=s+(4,5,6)>>>s(1,2,3,4,5,6)The tuple has changed. I thought I read that tuples are sequences (like lists), but they are immutable - They can't be changed once created. Could someone explain please ? _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 Sudip Bhattacharya wrote: >>>> s=(1,2,3) >>>> s=s+(4,5,6) >>>> s > (1,2,3,4,5,6) > > The tuple has changed. No it hasn't. You have created a *new* tuple, and assigned it to the same name. Consider: py> s = (1, 2, 3) py> id(s) 3083421332 py> t = s py> id(t) 3083421332 This shows that both s and t are names for the same tuple, with ID 3083421332. Now watch when we use += on s: py> s += (4, 5) py> id(s) 3083534812 py> id(t) 3083421332 py> t (1, 2, 3) The ID of s has changed, but t stays the same. So the original tuple remains untouched, and a new tuple is created. If you do the same thing with lists, you will see that because lists are mutable, it does *not* create a new list, but changes the original in place: py> a = [1, 2, 3] py> b = a py> a += [4, 5] py> b [1, 2, 3, 4, 5] -- Steven _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 In reply to this post by Sudip Bhattacharya On 8 March 2012 11:11, Sudip Bhattacharya <[hidden email]> wrote: >>>> s=(1,2,3) >>>> s=s+(4,5,6) >>>>s > (1,2,3,4,5,6) > > The tuple has changed. No, the tuple hasn't changed.  That's a *new* tuple.  Consider: Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> s=(1,2,3) >>> id(s) 44137280L >>> s=s+(4,5,6) >>> id(s) 34277416L >>> l=[1,2,3] >>> id(l) 44152072L >>> l.extend([4,5,6]) >>> id(l) 44152072L >>> l [1, 2, 3, 4, 5, 6] >>> l=l+[7,8,9] >>> id(l) 44150856L >>> l [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> So, as you can see, in the case of the tuple, the addition actually creates a *new* tuple (different id) that consists of the members of the 2 input tuples, whereas in the case of the list, since the list is mutable, the same list (same id) is *changed* when you add another list to it. Walter _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 In reply to this post by Sudip Bhattacharya On 8 March 2012 18:11, Sudip Bhattacharya <[hidden email]> wrote: >>>> s=(1,2,3) >>>> s=s+(4,5,6) >>>>s > (1,2,3,4,5,6) > > The tuple has changed. > > I thought I read that tuples are sequences (like lists), but they are > immutable - They can't be changed once created. Could someone explain please > ? I'm just a noob, but as nobody else has replied yet...... As far as I *think* I know: s=s+(4,5,6) creates a new tuple, it doesn't change it. Tuples are immutable as in you can't add or append and stuff. Also: >>> s[0] = 8 Traceback (most recent call last):   File "", line 1, in TypeError: 'tuple' object does not support item assignment Nor does it have attribute "reverse" or anything like that. HTH Col _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 In reply to this post by Sudip Bhattacharya Just to add, notice that even for lists, l = l + [7,8,9] produces a new list, while l += [7,8,9] does not. _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 On 8 March 2012 18:27, Walter Prins <[hidden email]> wrote: > Just to add, notice that even for lists, > > l = l + [7,8,9] > > produces a new list, while > > l += [7,8,9] > > does not. > _______________________________________________ > Tutor maillist  -  [hidden email] > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutorI was just thinking about the immutability of things and tried this (which -at least I- find interesting: >>> id(1) 154579120 >>> a = 1 >>> id(a) 154579120 >>> a += 2 >>> id(a) 154579096 >>> id(3) 154579096 >>> a is 3 True >>> Although there is probably no other logical way of doing it - I learnt something new again! _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 In reply to this post by col speed That makes perfect sense. What happened was that the old tuple got replaced with a new tuple (old + new items) and NOT changed cause tuple is immutable. Thanks HTH. On Thu, Mar 8, 2012 at 4:53 PM, col speed wrote: On 8 March 2012 18:11, Sudip Bhattacharya <[hidden email]> wrote: >>>> s=(1,2,3) >>>> s=s+(4,5,6) >>>>s > (1,2,3,4,5,6) > > The tuple has changed. > > I thought I read that tuples are sequences (like lists), but they are > immutable - They can't be changed once created. Could someone explain please > ? I'm just a noob, but as nobody else has replied yet...... As far as I *think* I know: s=s+(4,5,6) creates a new tuple, it doesn't change it. Tuples are immutable as in you can't add or append and stuff. Also: >>> s[0] = 8 Traceback (most recent call last):  File "", line 1, in TypeError: 'tuple' object does not support item assignment Nor does it have attribute "reverse" or anything like that. HTH Col -- Thanks and regards,Sudip BhattacharyaMobile: +91 9999 100 706 Home Land line: +91 11 22237561Office Land line: +91 0124 4321078 eMail ID: [hidden email]; [hidden email]Please visit my website at: www.sudipb.com _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 In reply to this post by col speed col speed wrote: > I was just thinking about the immutability of things and tried this > (which -at least I- find interesting: > >>>> id(1) > 154579120 >>>> a = 1 >>>> id(a) > 154579120 >>>> a += 2 >>>> id(a) > 154579096 >>>> id(3) > 154579096 >>>> a is 3 > True > Although there is probably no other logical way of doing it - I learnt > something new again! Prepare to have your mind boggled: py> a = 99 py> b = 99 py> a is b True py> a = 9912345 py> b = 9912345 py> a is b False Well, okay, so it's not *much* of a boggle. Perhaps a bogglet. What happens is that Python caches the small integers, like -1, 0, 1, up to some limit, and re-uses them when and as needed. That limit various from version to version, so you can't rely on it. But larger integers are not cached, and so you get a fresh one each time. This makes sense, and is easy to understand. Now for the real boggle: py> a = 9912346; b = 9912346 py> a is b True Can you guess what is going on here? (Answer will follow later.) -- Steven _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 On 8 March 2012 18:51, Steven D'Aprano <[hidden email]> wrote: > col speed wrote: > >> I was just thinking about the immutability of things and tried this >> (which -at least I- find interesting: >> >>>>> id(1) >> >> 154579120 >>>>> >>>>> a = 1 >>>>> id(a) >> >> 154579120 >>>>> >>>>> a += 2 >>>>> id(a) >> >> 154579096 >>>>> >>>>> id(3) >> >> 154579096 >>>>> >>>>> a is 3 >> >> True >> Although there is probably no other logical way of doing it - I learnt >> something new again! > > > Prepare to have your mind boggled: > > py> a = 99 > py> b = 99 > py> a is b > True > py> a = 9912345 > py> b = 9912345 > py> a is b > False > > > Well, okay, so it's not *much* of a boggle. Perhaps a bogglet. > > What happens is that Python caches the small integers, like -1, 0, 1, up to > some limit, and re-uses them when and as needed. That limit various from > version to version, so you can't rely on it. But larger integers are not > cached, and so you get a fresh one each time. > > This makes sense, and is easy to understand. Now for the real boggle: > > py> a = 9912346; b = 9912346 > py> a is b > True > > Can you guess what is going on here? > > (Answer will follow later.) > > > > -- > Steven Because it's on the same line and there are 2 variables with the same int, it gets cached??? Otherwise I'm truly boggled(a normal state of mind for me). _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] Tuple - Immutable ?

 In reply to this post by Steven D'Aprano-8 From: Steven D'Aprano <[hidden email]> To: [hidden email] Sent: Thursday, March 8, 2012 7:51:33 AM Subject: Re: [Tutor] Tuple - Immutable ? col speed wrote:> I was just thinking about the immutability of things and tried this> (which -at least I- find interesting:> >>>> id(1)> 154579120>>>> a = 1>>>> id(a)> 154579120>>>> a += 2>>>> id(a)> 154579096>>>> id(3)> 154579096>>>> a is 3> True> Although there is probably no other logical way of doing it - I learnt> something new again!Prepare to have your mind boggled:py> a = 99py> b = 99py> a is bTruepy> a = 9912345py> b = 9912345py> a is bFalseWell, okay, so it's not *much* of a boggle. Perhaps a bogglet.What happens is that Python caches the small integers, like -1, 0, 1, up to some limit, and re-uses them when and as needed. That limit various from version to version, so you can't rely on it. But larger integers are not cached, and so you get a fresh one each time.This makes sense, and is easy to understand. Now for the real boggle:py> a = 9912346; b = 9912346py> a is bTrueCan you guess what is going on here?(Answer will follow later.)I'll preface my answer by stating that I'm a noob and haven't checked the documentation for my answer, but I would guess that Python is saving the variables as integer values and that the larger values would require them being saved as doubles.-- Steven_______________________________________________Tutor maillist  -  [hidden email]To unsubscribe or change subscription options:http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor