Three, python memory garbage collection and a collection of strings

Variable declaration:
name1 = "andy"
name2 = name1
 
This time I changed the value to name1 "tom", now ask what is the value name2? why?
A: andy, because you change the value of the equivalent of name1 name1 you will point to a new memory address called a tom, but still points to the old andy name2 memory address (string). Similarly java and c #
as follows
>>> name1 = "andy"
>>> name2 = name1
>>> id(name1)
47729824
>>> id(name2)
47729824
>>> name1 = "tom"
>>> id(name1)
47894176
>>> id(name2)
47729824
 
However, if the value is a collection of name1, then the situation is the opposite, that it is making changes in the original memory address if the value has changed name1, name2 values ​​will change along with
>>> name_list = ["rooney","linda","ramos"]
>>> id(name_list)
46820168
>>> name_list.append('pique')
>>> id(name_list)
46820168
 
to sum up:
Let me talk about strings: That you to change the string, not to say change your strings are specified in the original memory address, but opened a new string to store a memory address changes.
Such as: andy into tom, not a change in the memory address of the original andy points, but added a memory address is stored
Besides the collection:
 

Guess you like

Origin www.cnblogs.com/steven9898/p/11329309.html