How strings are stored in python memory model -


i c background , beginner in python. want know how strings stored in memory in case of python.

i did like

s="foo"  id(s)=140542718184424  id(s[0])= 140542719027040 id(s[1])= 140542718832152 id(s[2])= 140542718832152 

i did not understand how each character getting stored in memory , and why id of s not equal id of s[0] (like use in c) , why id of s1 , s2 same?

python has no characters. indexing string creates new string, (like every other object) promptly vanquishes if don't keep reference around. id()s in example can't compared each other, object's id unique long object lives. in particular, id(s[0]) != id(s) because former new (temporary) object, , id(s[1]) == id(s[2]) because after first operand evaluated, first temporary string destroyed , second temporary string allocated freed memory. latter implementation detail , coincidence , cannot relied on.

reasoning string memory further complicated implementation details small strings (along integers, tuples, , more) being interned, some_str other_str may true equal strings come different sources (e.g. indexing string different indices).


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -