In Python a tuple is immutable object but allows mutation of a contained list? -


in python:

>>> tup = (1,"a",[1,2,3]) >>> tup (1, 'a', [1, 2, 3]) >>> tup[2][1] = "a" >>> tup (1, 'a', [1, 'a', 3]) 

from above modify list contents part of tuple. since tuple immutable how possible? did understood immutable part in wrong way?

you did understand immutable part wrong way. tuple indeed immutable, cannot change list:

in [3]: tup[2] = [] --------------------------------------------------------------------------- typeerror                                 traceback (most recent call last) <ipython-input-3-02255d491606> in <module>() ----> 1 tup[2] = []  typeerror: 'tuple' object not support item assignment 

the reference list immutable in tuple. however, list mutable, , hence can modified, know.

in other words, tuple, immutable, contains 3 elements:

  • integer 1
  • string "a"
  • a reference list.

you can imagine adress of list, if know c. if don't know memory, can see "the tuple knows where find list". doesn't contain list itself.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -