Python variables and object IDs

Introduction

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.

Check the ID of the object

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.

Pass to function

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.

Summary

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

Python variables and object IDs
Python: Class and instance variables
About Python variables and objects
[Python] Variables
Python built-in object
Python built-in object
Python variables and data types learned in chemoinformatics
I tried object detection using Python and OpenCV
[python] Difference between variables and self. Variables in class
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
Practice applying functions and global variables in Python
Trouble with Python pseudo-private variables and class inheritance
Generate and output plantuml object diagram from Python object
[python] Compress and decompress
Python memorandum numbering variables
[Python] Get environment variables
Python and numpy tips
Global and local variables 2
[Python] pip and wheel
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
python input and output
Python and Ruby split
Object oriented in python
About Python3 ... (Ellipsis object)
Python3, venv and Ansible
Python asyncio and ContextVar
Global and local variables 1
Getting Started with python3 # 2 Learn about types and variables
Difference between Ruby and Python in terms of variables
[Python] Start a batch file from Python and pass variables.
Programming with Python and Tkinter
Encryption and decryption with Python
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Install Python and Flask (Windows 10)
About python objects and classes
Temporarily save a Python object and reuse it in another Python
[python] Value of function object (?)
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
Null object comparison in Python
Understand Python packages and modules
[Python] How to play with class variables with decorator and metaclass
Python notes using perl-special variables
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Python and ruby slice memo
Handle environment variables in Python
Python installation and basic grammar
I compared Java and Python!
Python shallow and deep copy
Reference order of class variables and instance variables in "self. Class variables" in Python
Display numbers and letters assigned to variables in python print
# 1 [python3] Simple calculation using variables
About Python, len () and randint ()