Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the id () result of Python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to understand the id () results of Python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to understand the id () results of Python"!

A classmate once asked me the question of id function, why the id value of the same list content is different, while the id value of the same number is the same, sometimes it is different?

In [51]: a = [1, 2, 5, 3]

In [52]: B = [1, 2, 5, 3]

In [53]: id (a)

Out [53]: 140424256381640

In [54]: id (b)

Out [54]: 140424256004872

In [55]: axi1

In [56]: bread1

In [57]: id (a), id (b)

Out [57]: (4454569376, 4454569376)

In [58]: axi1000

In [59]: baked 1000

In [60]: id (a), id (b)

Out [60]: (1404243121552,1404243118928)

In fact, there is an explanation for this question in the frequently asked questions and answers in the official documents:

Id () returns an integer that is guaranteed to be unique during the lifetime of the object. Because in CPython, this is the memory address of the object, it often happens after the object is deleted from memory, and the next newly created object is assigned to the same location in memory. This example illustrates this point:

Id (1000)

13901272

Id (2000)

13901272

These two id belong to different integer objects created earlier and are deleted immediately after the id () call is executed. To ensure that the object whose id you want to check is still active, create another reference to the object:

> a = 1000; b = 2000

> > id (a)

13901272

> > id (b)

13891296

Here, part of the problem has been solved through the relationship between memory and objects, and our top problem has not been completely solved. Let's take a look at DE8UG's further explanation.

Whether it is strings, numbers, lists, and other objects, eventually all data must have its own location in memory. Generally speaking, we only need to know the memory address to get the specific data, that is, the memory problem dealt with by ourselves after id, and the id values of different data objects are different if we watch it directly. One difference is that the underlying c language code of python caches addresses in a certain range of numbers, and the id values seen in this range are the same, refer to: https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong:

The current implementation keeps an array of integer objects for all integers between-5 and 256, when you create an int in that range you actually just get back a reference to the existing object.

Integers in the range of-5 to 256 directly refer to existing objects, so the address is the same.

Let's do a test with two critical values:

In [62]: Aspen Murray 5

In [63]: baked Murray 5

In [64]: id (a), id (b)

Out [64]: (4454569184, 4454569184)

In [65]: baked Murray 6

In [66]: Aspen Murray 6

In [67]: id (a), id (b)

Out [67]: (1404243121904, 140424243121552)

In [68]: await 256

In [69]: baked 256

In [70]: id (a), id (b)

Out [70]: (4454577536, 4454577536)

In [71]: astat257

In [72]: baked 257

In [73]: id (a), id (b)

Out [73]: (140424253951760140424253950320) so far, I believe you have a deeper understanding of "how to understand the id () result of Python". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report