Variable in Python / immutable type variables
And c / c ++ different, Python / Java variables in all reference types, no value types
Python assignment statement consists of three parts, for example:
int a = 1
Type identification value
Identification (identity): used to uniquely identify an object (pointing to the object memory address); Type (type); value (value)
Python uses the ID to access the value of an object, that object is the equivalent of the Python c pointers
Reference (reference): memory address of the object
Variable Type: changes directly in memory of the original object re-assignment, variable references unchanged. float, list, dict
When you create reassigned a new object in memory while changing a variable reference: immutable types. int, char, tuple
This is done to improve efficiency, such as 1,2 such variables can often be used, once defined, then the efficiency of multiple references will be higher
Know the Python memory variables, you can avoid some of the pit. For example and a, b are the type of List, so that after a = b, for a, b modify any List, a List can affect additional value, because they actually refer to the same memory address
To distinguish between "objects (object)" and "variable (variable)", the object is created in the program, stored in memory, the variable is a reference to the object (reference), variable assignment is to allow a variable to an object, an object can It is directed to a number of variables
python All data types are objects
Mutable objects (lists, dictionaries, collections) changes will affect all the variables point to the object
Immutable objects (string, integer, tuple), the object point values of all the variables is always the same, it does not change, a new object will return its value by a certain operating Update [Once created, unchangeable]
Variables can be deleted, however, must not only be recycled garbage collection mechanism
1 l1=[1,2,3] 2 print(id(l1)) 3 l1.append(4) 4 l2=[1,2,3] 5 l3=l2 6 print(id(l1)) 7 print(id(l2)) 8 print(id(l3)) 9 10 v1=1 11 print(id(v1)) 12 v1=2 13 v2=1 14 v3=v2 15 print(id(v1)) 16 print(id(v2)) 17 print(id(v3))
Output:
154977800
154977800
138660424
138660424
8791557861120
8791557861152
8791557861120
8791557861120
Visible, variable objects every time you create a new object is created, then change will affect all the variables pointing to it
1 test=[1] 2 def change1(a): 3 a = [2] 4 print(id(a)) 5 def change2(a): 6 a.append(3) 7 print(id(a)) 8 print(id(test)) 9 change1(test) 10 change2(test) 11 print(test)
Output:
154977800
128902536
154977800
[1, 3]
The first function is reassigned second function is to change the original value of the target object belongs to self variable object
1 class Dog(): 2 def __init__(self,name,age): 3 self.name = name 4 self.age = age 5 6 dog1 = Dog('Sam',2) 7 print(id(dog1)) 8 dog2 = Dog('Tom',1) 9 print(id(dog2)) 10 dog1.name = 'Petter' 11 print(id(dog1))
Output:
155199584
155198408
155199584
Self-built objects belonging to the object variable
reference:
https://www.cnblogs.com/blackmatrix/p/5614086.html
https://blog.csdn.net/u012995500/article/details/82764128