Study notes for First Python 3rd Edition
--Python without type declaration --Shared reference --Shared reference and object override --"Equivalent" and "Same"
a = 3
--Variables are created as soon as the value is assigned --If an assignment is made after creation, the already assigned value will be replaced with a new one. --In this case, the variable has already been created, so it is not newly created.
--The variable itself has no type information --It is the "object" that has the type information that corresponds to the variable. --Variables only have a reference to the corresponding object (the data being assigned at that time)
--All replaced with the corresponding object --Variables that do not have a corresponding object cannot be used --Variables and objects are stored and linked in different parts of memory
--When a new object is assigned to a variable, the previously assigned object is destroyed and the memory area occupied by that object is released **, which is called ** garbage collection **. Call
a = 3
b = a
How is this
In this way, ** the situation where multiple variables are references to the same object ** is called ** shared reference **.
Then add a line to the previous code
a = 3
b = a
a = 'spam'
This is like this The variable a serves as a reference to the newly created string object'spam', but the variable b remains a reference to the object 3.
A list is an array of objects in square brackets that can be overwritten.
In the example below, L2 remains a reference to [2, 3, 4]
L1 = [2, 3, 4]
L2 = L1
L1 = 24
In the following example, the value of the reference object is overwritten.
#Variable object
>>> L1 = [2, 3, 4]
#Make another reference to the same object
>>> L2 = L1
#Overwriting elements
>>> L1[0] = 24
#Changes have been made to the list corresponding to L1
>>> L1
[24, 3, 4]
#It also affects L2!
>>> L2
[24, 3, 4]
If you copy it by the following method, L2 will not be changed, and ** the two variables will indicate different memory areas **.
#Variable object
>>> L1 = [2, 3, 4]
#Make a copy of L1
>>> L2 = L1[:]
#Overwriting elements
>>> L1[0] = 24
#Changes have been made to the list corresponding to L1
>>> L1
[24, 3, 4]
#L2 does not change
>>> L2
[2, 3, 4]
>>> x = 42
#Will 42 be destroyed soon?
>>> x = 'shrubbery'
** In Python, small integers and strings with a small number of characters are cached and reused **
-== The operator compares whether an object is "equivalent" --The is operator is for comparing whether objects are "identical" -** The conditions for being "identical" are stricter than "equivalent" **
>>> L = [1, 2, 3]
#M and L are references to the same object
>>> M = L
#Comparison of whether both are "equivalent"
>>> L == M
True
#Comparison of whether they are "identical"
>>> L is M
True
>>> L = [1, 2, 3]
#The objects that M and L correspond to are different
>>> M = [1, 2, 3]
#Both values are the same
>>> L == M
True
#Two objects are equivalent but not identical
>>> L is M
False
--In the following cases, X and Y are equivalent but not the same --But small numbers and strings are cached and reused, so they both serve as references for the same object.
>>> X = 42
#The two 42s should not be the same
>>> Y = 42
>>> X == Y
True
#The two will be the same because they are cached!
>>> X is Y
True
Recommended Posts