Skip to content Skip to sidebar Skip to footer

Understanding Python Name Objects, Immutable Data, And Memory Management

Newbie here. Say I do the following: a = '123' b = a[1] a = 'abc' My understanding is that: a is not a string object. '123' is a string object (not sure I'm using 'object' word c

Solution 1:

To sum up comments and give an structured explanation:

"123"

is a string object and is immutable. the char at operation ( [x] ) on a string object in Python return a copy of the letter. A single letter is also a string.

a = "123"
b = a[1]

You now have 2 independent string objects "123" and "2". Since b is assigned to an independent string object, changing a will have no effect to b. If you reassign a however, your string object "123" will be lost and there is no way to revive "1" and "3" sorry but you probably already know that.

the list object works differently.


Post a Comment for "Understanding Python Name Objects, Immutable Data, And Memory Management"