Clarification on Immutability please

MRAB python at mrabarnett.plus.com
Tue Jan 21 21:41:13 EST 2020


On 2020-01-22 01:52, Ethan Furman wrote:
> On 01/21/2020 10:55 AM, Michael Torrie wrote:
> 
>> Slicing
>> returns a new object whether one is slicing a tuple, list, or a string,
>> the latter two are mutable objects.
> 
> Strings are not mutable.
> 
In the case of tuples and strings, if the slicing encompasses the entire 
object, the original object is returned:

 >>> t = (0, 1, 2)
 >>> t[:] is t
True

 >>> s = "012"
 >>> s[:] is s
True

Those are safe optimisations because they are immutable objects.


More information about the Python-list mailing list