Quote:In scenario-1, I assume blist[2] = hex 0B = decimal 11 is stored in memory.
id()
show location in memory.So
bytes
string are immutable,same as str
string type.Nothing get changed in memory,not allowed.
>>> b_string = b'\x76\x12\x0B\x08' >>> [id(i) for i in b_string] [1400293216, 1400291616, 1400291504, 1400291456] >>> b_string[2] = 10 Traceback (most recent call last): File "<string>", line 301, in runcode File "<interactive input>", line 1, in <module> TypeError: 'bytes' object does not support item assignment >>> [id(i) for i in b_string] [1400293216, 1400291616, 1400291504, 1400291456]
Quote:In scenario-2, I assume blist[2] is also hex 0B = decimal 11 stored in memory.List are mutable so here get item 2 changed,the original 11(with memory location 1400291504),
will be garbage collect and replaced with 10(new memory location 1400291488).
>>> blist = list(b'\x76\x12\x0B\x08') >>> [id(i) for i in blist] [1400293216, 1400291616, 1400291504, 1400291456] >>> blist[2] = 10 >>> [id(i) for i in blist] [1400293216, 1400291616, 1400291488, 1400291456]As mention you don't have to worry about how things are stored in memory.
Python clean up(garbage collect) stuff in memory for you