Check the behavior when assigning Python

Summary

Behavior confirmation when substituting

Assign an int variable

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

Substitute a new variable 3 for b

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

If the variable is list

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

Assignment of list to element

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

Assignment of dict type elements

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

Check the behavior when assigning Python
Check the behavior of destructor in Python
[Python] Check the installed libraries
[Python] Check the current directory, move the directory
Behavior when listing in Python heapq
Master the type in Python? (When should type check be done)
Behavior when returning in the with block
Check the existence of the file with python
[python, multiprocessing] Behavior for exceptions when using multiprocessing
Check if the URL exists in Python
Check the path of the Python imported module
python grammar check
Check items when the imported python module does not work as expected
[python] Check the elements of the list all, any
Check if the characters are similar in Python
When do python functions affect the caller's arguments?
[python, CPython] GC behavior when throwing an exception
Check the date of the flag duty with Python
[Python] Precautions when assigning values to multidimensional arrays
Python Note: When assigning a value to a string
About the behavior of Model.get_or_create () of peewee in Python
Behavior when saving python datetime object in MongoDB
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
Check if the string is a number in python
Find the maximum Python
Easy way to check the source of Python modules
Python note: When the pip command cannot be used
Import audit.log into Splunk and check the behavior when Splunk is started for the first time
Initial settings when using the foursquare API in python
Python default argument behavior
[python] behavior of argmax
the zen of Python
Check types_map when using mimetypes on AWS Lambda (Python)
blender, python, sphere behavior
Specifies the function to execute when the python program ends
Check when the version does not switch with pyenv
Domain check with Python
[Python] Split the date
About the --enable-shared option when building Python on Linux
Check Python # type identity
Check the asymptotic nature of the probability distribution in Python
Check version with python
Python Note: The mystery of assigning a variable to a variable
Check the argument type annotation when executing a function in Python and make an error
[python] How to check if the Key exists in the dictionary
Check the operation of Python for .NET in each environment
Solution when the image cannot be displayed with tkinter [python]
Tank game made with python About the behavior of tanks
Behavior when Linux less ends depending on the connection source
Check the scope of local variables with the Python locals function.
Check in advance what happens when you execute the command
[Python: UnicodeDecodeError] One of the error solutions when reading CSV
Check when the Docker container does not connect to the Internet
Solve the Japanese problem when using the CSV module in Python.
python3 Measure the processing speed.
Download the file in Python
Story when iterating python tuple
Check python coverage with pytest-cov
Find the difference in Python
Compare the Python array-like guys
About the Python module venv