a = 1
b = 2
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
a = b #id of a replaces id of b
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
result
a: 1 b: 2
id_a: 93893417024192 id_b: 93893417024224
a: 2 b: 2
id_a: 93893417024224 id_b: 93893417024224
b = 3 #The id of b is replaced by the id of 3,The id of a remains the same
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
result
a: 2 b: 3
id_a: 93893417024224 id_b: 93893417024256
a = [1,1]
b = [2,2]
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
a = b #id of a replaces id of b
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
b = [3,3] #The id of b is replaced by the id of 3,The id of a remains the same
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
result
a: [1, 1] b: [2, 2]
id_a: 140361960710984 id_b: 140361960208392
a: [2, 2] b: [2, 2]
id_a: 140361960208392 id_b: 140361960208392
a: [2, 2] b: [3, 3]
id_a: 140361960208392 id_b: 140361960256584
a = [1,1]
b = [2,2]
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
print("id_a[0]:", id(a[0]), "id_b[0]:", id(b[0]))
a = b #id of a replaces id of b
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
print("id_a[0]:", id(a[0]), "id_b[0]:", id(b[0]))
b[0] = 3 # b[0]Id is replaced by id 3 and at the same time a[0]Id is also replaced by id of 3,The ids of a and b remain the same
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
print("id_a[0]:", id(a[0]), "id_b[0]:", id(b[0]))
result
a: [1, 1] b: [2, 2]
id_a: 140361961620360 id_b: 140361958272904
id_a[0]: 93893417024192 id_b[0]: 93893417024224
a: [2, 2] b: [2, 2]
id_a: 140361958272904 id_b: 140361958272904
id_a[0]: 93893417024224 id_b[0]: 93893417024224
a: [3, 2] b: [3, 2]
id_a: 140361958272904 id_b: 140361958272904
id_a[0]: 93893417024256 id_b[0]: 93893417024256
When assigning directly inside the dict, the id of the dict does not change and the id of the assigned dict element is replaced. At this time, if there is a variable that shares the id of dict, the id of dict does not change, so the dict element is changed.
a = {0:1}
b = {0:2}
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
print("id_a[0]:", id(a[0]), "id_b[0]:", id(b[0]))
a = b #id of a replaces id of b
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
print("id_a[0]:", id(a[0]), "id_b[0]:", id(b[0]))
b[0] = 3 # b[0]Id is replaced by id 3 and at the same time a[0]Id is also replaced by id of 3,The ids of a and b remain the same
print("a:",a,"b:",b)
print("id_a:", id(a), "id_b:", id(b))
print("id_a[0]:", id(a[0]), "id_b[0]:", id(b[0]))
result
a: {0: 1} b: {0: 2}
id_a: 140362018772456 id_b: 140361986516168
id_a[0]: 93893417024192 id_b[0]: 93893417024224
a: {0: 2} b: {0: 2}
id_a: 140361986516168 id_b: 140361986516168
id_a[0]: 93893417024224 id_b[0]: 93893417024224
a: {0: 3} b: {0: 3}
id_a: 140361986516168 id_b: 140361986516168
id_a[0]: 93893417024256 id_b[0]: 93893417024256
Reference: [Programming FAQ — Python 3.8.1rc1 Documentation](https://docs.python.org/ja/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call -by-reference) Pass by value and pass by reference in C #-Qiita Why shouldn't we use the word "pass by reference" in Java, JavaScript, Python, Ruby, etc.? --Qiita I can't say it's passed by reference anymore-Qiita
Recommended Posts