Here, I would like to talk about "references" in the sense that I want you to know the characteristics of the list, rather than explaining new knowledge.
First, let's check the reference. Enter the following code in the ** Python Console **.
>>>Lx = [8, 6, 5, 9, 7]
>>>Ly = Lx
>>>Ly
[8, 6, 5, 9, 7]
In this state, enter the following code.
>>>Ly[4] = 1000
>>>Ly
[8, 6, 5, 9, 1000]
I think you can understand up to this point. Briefly, assign the list [8, 6, 5, 9, 7] to the variable ** Lx **, assign it to Ly, then assign 1000 to Ly [4] to get the contents. Confirming. Of course, if you display Ly, it will be [8, 6, 5, 9, ** 1000 **].
Now, let's display ** Lx ** in this state. Then it will be as follows.
>>>Lx
[8, 6, 5, 9, 1000]
You should have changed the elements of the ** Ly ** list earlier, but you can see that ** Lx ** has also changed.
I would like to take a look behind the scenes.
First,
>>>Lx = [8, 6, 5, 9, 7]
Regarding, I am assigning a list to Lx. The important thing here is [Chapter 02 "Variables"](https://qiita.com/ko0821/items/8355b4e192aa76a0e8ae#%E5%A4%89%E6%95%B0%E9%87%8D%E8% As I explained at the time of A6% 81), I mentioned that ** variables are tags **.
Again, tagging [8, 6, 5, 9, 7] in the list with ** Lx ** is the same as explained in Chapter 02. The figure is as follows.
Then next
>>>Ly = Lx
>>>Ly
[8, 6, 5, 9, 7]
Let's take a look at. Here, regarding ** Ly = Lx **, when this assignment is made, it means that ** see what the variable Lx refers to as well as the variable Ly **.
Then, I think it can be represented in the figure below.
** Lx ** and ** Ly ** are ** referencing the same list **. Therefore, when Ly is output, [8, 6, 5, 9, 7] is output.
Then in that state,
>>>Ly[4] = 1000
>>>Ly
[8, 6, 5, 9, 1000]
This section explains.
Since the entity that Ly refers to is [8, 6, 5, 9, 7], if Ly [4] = 1000, the operation is for the entity [8, 6, 5, 9, 7]. , [8, 6, 5, 9, ** 1000 **].
Therefore, it can be represented by the following figure.
In such a state, if you enter ** Lx **,
>>>Lx
[8, 6, 5, 9, 1000]
And since the entity that refers to ** Ly ** is the same as ** Lx **, the output result of ** Lx ** will also change.
Not limited to the references in this list, references will appear in various places in the future. It is commonly referred to as ** object reference **.
(*) </ font> Those who have mastered the Java language may be familiar with this concept of reference. I think the same thing happened in "array".
So far, we have explained with "assignment" such as "assigning a value to a variable called ** x **". From now on, we will use expressions such as "variable ** x ** refers to an entity (object)" and "variable ** x ** indicates".
Until now, the variables ** Lx ** and ** Ly ** referred to the list (object), and by referring to it, the contents of the actual list could be confirmed.
Now, with the above situation in mind, enter the following code from the ** Python Console **.
>>>Lx = 10
>>>Ly = ['Japan', 'Canada']
What I want to say here is what happens to the actual list [8, 6, 5, 9, 1000].
Since the variables ** Lx ** and ** Ly ** refer to different entities, we cannot reach the list of entities [8, 6, 5, 9, 1000].
If this is left as it is, the memory of the computer will be full. To avoid this, Python determines that it is "garbage data" and deletes the actual list [8, 6, 5, 9, 1000].
This is called ** garbage collection **.
This time I have described the features of the list. To be honest, I don't use "reference" in practice, but it's something that can be found in any Python book.
Those who are thinking of choosing Python for the Fundamental Information Technology Engineer Examination are likely to be asked in the future, so be sure to remember it.
Recommended Posts