+ 1

ID of unassigned list value

I see in python that ID of unassigned list value is always the same. For example id([1]) and id([1,2]) is same. How does it define this value and where I can read about such behavior?

23rd Jul 2020, 10:48 AM
Ashish
Ashish - avatar
2 Respuestas
+ 4
It doesn't matter if you assign a list somewhere or not. As soon as you write a list literal, the list will be created. That means, Python will take memory for that and give an id. The only difference is that the object will be garbage-collected after the statement is over, if you don't put a reference to it. So list is created... ... destroyed... ... and the now free id is given to the new list. At least that's what I'd suspect. Try this: print(id([])) # a = [] print(id([])) Id is the same. Now if you uncomment line 2, the id differs.
23rd Jul 2020, 10:53 AM
HonFu
HonFu - avatar
+ 2
Its a ways down there but there's a lot of good information here on the whole https://towardsdatascience.com/JUMP_LINK__&&__python__&&__JUMP_LINK-variable-assignment-9f43aed91bff
23rd Jul 2020, 10:50 AM
Slick
Slick - avatar