Python immutable type int memo

The int type is an immutable type. Therefore, the value once stored in memory cannot be changed.

If the values are the same, for example if there are multiple variables to which 1 is assigned, then all of those variables will refer to only one memory that stores 1. Will memory that is no longer referenced by variables be deleted? (Investigation required) (Will it be like a swift reference counter?)

qiita.rb


>>> int1 = 1
>>> int2 = 1
>>> print(id(int1), id(int2))#Output reference memory id
4383344848 4383344848
>>> int2 = 3
>>> print(id(int1), id(int2))
4383344848 4383344912
>>> int2 = 1
>>> print(id(int1), id(int2))
4383344848 4383344848


>>> intList = [1,2,3,4,5]
>>> print(id(intList[0])) #Index to store 1
4383344848

>>> print(id(intList[2])) #"3"Reference memory id
4383344912 #I just assigned it to int2"3"It's the same as the id of.(The number of reference variables for 3 has become zero, but I wonder if the memory will be cleaned immediately.)

Addendum 2020/10/04:

Will memory that is no longer referenced by variables be deleted? (Investigation required) (Will it be like a swift reference counter?) It's the same as the id of "3" that was assigned to int2 earlier. (The number of reference variables for 3 was zero, but I wonder if the memory will be cleaned immediately.)

You commented on the above answer in the comments section. Thank you @shiracamus.

Recommended Posts

Python immutable type int memo
Python data type summary memo
python memo
Python memo
python memo
Python memo
Python memo
Python memo
[Personal memo] Python sequence type / mapping type
[Python] Memo dictionary
Python numeric type
python beginner memo (9.2-10)
python beginner memo (9.1)
★ Memo ★ Python Iroha
[Python] EDA memo
Python 3 operator memo
[My memo] python
Python2 string type
Python3 metaclass memo
Python # string type
[Python] Basemap memo
Python beginner memo (2)
[Python] Numpy memo
python memo: Treat lists as a set type
python int is infinite
Python class (Python learning memo ⑦)
python openCV installation (memo)
Python module (Python learning memo ④)
Visualization memo by Python
Python callable type specification
Python test package memo
[Python] Memo about functions
python regular expression memo
Binary search (python2.7) memo
[My memo] python -v / python -V
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
DynamoDB Script Memo (Python)
Python basic memo --Part 2
python recipe book Memo
Basic Python command memo
Python OpenCV tutorial memo
Python basic grammar memo
TensorFlow API memo (Python)
Check Python # type identity
python useful memo links
Python decorator operation memo
Python basic memo --Part 1
Effective Python Memo Item 3
Divisor enumeration Python memo
Python --Notes when converting from str type to int type
Python memo using perl-Dictionary type (case is not valid)
Python memo (for myself): Array
Python exception handling (Python learning memo ⑥)
Python execution time measurement memo
Convert numpy int64 to python int
[Line / Python] Beacon implementation memo
Python --Check type of values
Python Basic Grammar Memo (Part 1)
Python code memo for yourself