• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Two list copies has same id

Resolved: Two list copies has same id

0
By Isaac Tonny on 16/03/2023 Issue
Share
Facebook Twitter LinkedIn

In this post, we will see how to resolve Two list copies has same id

Question:

As I know, using “=” for copying objects actually just creates another reference to the same object. So if I do
my output is 2367729946880 2367729946880 True, which is fine and obvious.
If I make copies of list, they has different ids:
Output: 2646790648192 2646790705984 False.
So far so good. Though, if I try creating copies directly in the print, they unexpectedly has the same id:
Output: 2209221063040 2209221063040
How does it happen?
I tried a bunch of different stuff, like:
  • assigning copies to variables in the same line in case there is some one-line optimization as Python is an interpreted language

Output: 2545996280192 2545996337984
  • passing copies to the function to avoid using “=”

Output: 1518673867136 1518673852736
  • passing copies to the function, using *args because as I know, print() gets arguments same way:

Output: 1764444352896 1764444338496 (difference in 3rd least valuable digit)
None seem to produce same behaviour. Even comparing ids using operator “is” prints False:
Output: False
But using “==” still gives True:
Output: True
Summing up all the text, I wonder about:
  1. What provokes this kind of behaviour? It doesn’t seem intended. Is it result of some optimization?
  2. Can it potentially lead to some nasty unexpected bugs? Is there another way to get two copies to have same id?

Best Answer:

id returns an integer that is unique for the lifetime of the object. Here, that id got re-used, b ecause the lifetime of the objects did not overlap. In the expression:
First, a.copy() is evaluated, it creates a new dict, that dict gets passed to id, id returns an integer, the dict is no longer referenced, and immediately reclaimed (this is a Cpython implementation detail). Then, a.copy() is evaluated again, and again, it is passed to id. It returns the same int because that is perfectly in-line with the documented function of id. You can look at the dissasembled bytecode and see how this works exactly:
Of course, you don’t get any information about when and where an object is garbage collected.
Another way to see similar behavior:
Those were all distinct list objects, but they were able to re-use the id because they have non-overlapping lifetimes.
So, this is all working as intended and documented.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Input Focus between two React Components

02/04/2023

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Resolved: Table data is coming as empty in React

02/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.