1. Variables
Variables are created the first time they are assigned, they must be assigned before they are used
The variable itself has no type, and the variable type is the object type it refers to;
The variable is replaced with the object it refers to when it is used
2. Objects
The object itself has a count and type, the variable refers to the object, and when the object's reference becomes 0, the object memory is reclaimed. But a small type of object, such as int, does not necessarily reclaim its memory immediately.
Modifying any of the variables that point to the same Mutable object will affect the object that is pointed to, that is, the values of the two variables will be modified.
>>> l=[1,2,9]>>> s= l>>> s[0]=11>>> s[, 2, 9] >>> l[, 2, 9]>>>
A copy between mutable objects:
1. Copy.copy a shallow copy copies only the parent object and does not copy the inner sub-objects of the object.
2. copy.deepcopy deep copy copy objects and their sub-objects
3. Can be used to determine whether 2 objects are the same
ImportCopya= [1, 2, 3, 4, ['a','b']]#Original Objectb= A#assignment, a reference to a passing objectc = Copy.copy (a)#object Copy, shallow copyD = Copy.deepcopy (a)#object Copy, deep copyA.append (5)#Modify Object AA[4].append ('C')#Modify the [' A ', ' B '] Array object in Object aPrint('a=', a)Print('b=', B)Print('c=', c)Print('d=', D)Print('A is B'+ F isb)Print('A is C'A isc)Print('D is a'D isA
Output Result:
a= [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
b= [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
c= [1, 2, 3, 4, [' A ', ' B ', ' C ']]
d= [1, 2, 3, 4, [' A ', ' B ']]
A is B True
A is C False
D is a False
Python Variable Object reference