This is the article on the 21st day of Inatatsu Adventar.
Today, let's talk about Python object IDs and ~~ passing by reference ~~ variables.
I'm a person who doesn't use Python very often, so if you make a mistake, please kindly correct it. .. .. .. .. ..
Python's Tips
--Python is all an object --Each object has an object ID --The object ID is assigned to the variable --The object ID is also passed to the argument (variable) when calling the function.
test.py
num1 = 1
num2 = 2
print(id(1)) # 1
print(id(num1)) # 1
print(id(1 + 1)) # 2
print(id(num1 + 1)) # 2
print(id(num2)) # 2
I tried to get IDs 1 and 2 in various ways.
$ python test.py
140280906740832
140280906740832
140280906740864
140280906740864
140280906740864
The output looks like this. Try running it again.
$ python test.py
140040431375456
140040431375456
140040431375488
140040431375488
140040431375488
The IDs 1,1,2,2,2 are output in order from the top, but the values output at the first and second times are different. And the object IDs of 1 and num1 are the same. From this, it can be seen that the object ID of Python is dynamically determined, and the object ID of the variable containing 1 and 1 is common.
Then it is the next experiment.
test2.py
num1 = 1
num2 = 2
print(id(1))
print(id(num1))
num1 += 1
print(id(num1))
print(id(num2))
Execution result
$ python test2.py
140413757891680
140413757891680
140413757891712
140413757891712
The num1 has changed from 1 to 2 between the 2nd and 3rd, and the object ID has also changed here.
Next experiment
test3.py
num = 1
list = [1]
print(num)
print(id(num))
num = 2
print(num)
print(id(num))
print(list)
print(id(list))
list[0] = 2
print(list)
print(id(list))
Execution result
$ python test3.py
1
140609807909984
2
140609807910016
[1]
140609798595120
[2]
140609798595120
Yes, the object ID changes when the number changes from 1 to 2, but the list does not change the object ID even if the value inside changes.
test4.py
number_list = [1,2,3,4,5,6,7,8,9,10]
def show (arg):
print(arg)
print(id(arg))
def update_list (arg):
arg[0] = 0
print(id(number_list))
show(number_list)
update_list(number_list)
show(number_list)
Execution result
$ python test4.py
140048610816560
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
140048610816560
[0, 2, 3, 4, 5, 6, 7, 8, 9, 10]
140048610816560
You can see that the object ID does not change even if you pass it to the function or rewrite the contents.
Python passes ~~ by reference ~~ object IDs, and immutable values such as numbers have unique object IDs ~~, so if you rewrite the value inside the function, the referenced object ID will change. , Although it passes a reference, it behaves like passing a real value. ~~ You can only assign to a variable, and assigning a variable will assign another object ID, which will not affect the calling object. Changing the internal state of a mutable object such as a list does not change the object ID of the list itself, so changes inside the function affect variables and objects with the same object ID. Of course, if you assign another list to a variable, it will have a different object ID and will not affect external objects.
Recommended Posts