How the reference of the python array changes depending on the presence or absence of subscripts

This time, it is an experiment to take a deeper look at what a variable points to. The version of python is 3.4.3.

Modify an object from another variable

Even if you assign the contents of a variable to another variable, it points to the same object. Check this by comparing the object ids.

mylist = [[0, "mike"], [1, "jane"]]
person1 = mylist[0]

print("mylist = %s" % mylist)
print("person1 = %s" % person1)
# =>
# mylist = [[0, 'mike'], [1, 'jane']]
# person1 = [0, 'mike']

#Make sure the two variables point to the same object
print("mylist[0] id = %s" % id(mylist[0]))
print("person1 id = %s" % id(person1))
print(id(mylist[0]) == id(person1))
# => True

#Save the current object id for later confirmation
person1_id = id(person1)

Try changing the elements of the array through another variable. Only when a subscript is specified

#person is mylist[0]Make sure it is an alias for
person1[0] = 100
print("mylist = %s" % mylist)
print("person1 = %s" % person1)
# =>
# mylist = [[100, 'mike'], [1, 'jane']]
# person1 = [100, 'mike']

The element has been changed successfully. However, if you don't use subscripts, you can't change the element and a new object is assigned.

#through pesron1 mylist[1]Attempt to change the array of
person1 = [5, "michel"]
print("mylist = %s" % mylist)
print("person1 = %s" % person1)
# =>
# mylist = [[100, 'mike'], [1, 'jane']]
# person1 = [5, 'michel']

person1 = 5
print("mylist = %s" % mylist)
print("person1 = %s" % person1)
# => 
# mylist = [[100, 'mike'], [1, 'jane']]
# person1 = 5

If you compare it with the object id when the elements of the previous array were included, you can see that the object has certainly changed.

#Compare with id before changing object
print(person1_id == id(person1))
# => False

What the variable points to

Let's illustrate the above phenomenon. First is the case of using subscripts. Note that when we use subscripts, we are not pointing to an array (list in this case), but to its elements. Since we just changed the reference (arrow) of the element of the array, the variable that was referencing the array remains the reference of the array.

python_index_01_150702.jpg

Next is the case without subscripts. In the previous experiment, in this case, changing the value from another variable did not affect the original variable. This is because I changed the reference to the array to a reference to a new object.

python_index_02_150702.jpg

Recommended Posts

How the reference of the python array changes depending on the presence or absence of subscripts
rsync Behavior changes depending on the presence or absence of the slash in the copy source
In Python, change the behavior of the method depending on how it is called
The story of how the Python bottle worked on Sakura Internet
Predict the presence or absence of infidelity by machine learning
How to update the python version of Cloud Shell on GCP
The process of repeatedly extracting an array differs slightly depending on the language ...
[python] How to sort by the Nth Mth element of a multidimensional array
I tried to predict the presence or absence of snow by machine learning.
Checklist on how to avoid turning the elements of numpy's array with for
[Python numpy] Dynamically specify the index of the array
Output in the form of a python array
At the time of python update on ubuntu
An easy way to pad the number with zeros depending on the number of digits [Python]
Think about how to program Python on the iPad
How to get the number of digits in Python
Difference in results depending on the argument of multiprocess.Process
[Python] Summary of how to specify the color of the figure
How to enjoy Python on Android !! Programming on the go !!
How much do you know the basics of Python?
The return value of len or unichr may change depending on whether it is UCS-2 or UCS-4.
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
Confirmed the difference in the presence or absence of random processing during mini-batch learning with chainer
the zen of Python
Put the latest version of Python on linux (Debian) on Chromebook
Differences in sys.path depending on how Python is started (v3.8.2)
[Hyperledger Iroha] Notes on how to use the Python SDK
[Jinja2] Changes in line breaks depending on the hyphen position
How to deploy the easiest python textbook pybot on Heroku
[Python] Calculate the angle consisting of three points on the coordinates
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.