Home > Backend Development > Python Tutorial > Why does the result of id() not seem to be unique in Python?

Why does the result of id() not seem to be unique in Python?

WBOY
Release: 2023-09-03 16:49:11
forward
871 people have browsed it

Why does the result of id() not seem to be unique in Python?

The id() method in Python returns the identity of the object, which is the unique ID of the specified object. Now, you might be wondering, what is this id() .

The id here is the memory address of the object, an integer, which ensures that the object is unique and constant during its life cycle. Two objects with non-overlapping lifetimes may have the same id() value.

grammar

id(object)
Copy after login

This object can be object, String, Number, List, etc.

Unique ID of the list object

Example

In this example, we will use id() to get the unique id of the list object -

myList = ["john", "tom", "henry", "mark"]
res = id(myList)
print(res)
Copy after login

Output

140571958913920
Copy after login

When we run it again, the id will be different:

140597372271552
Copy after login

The unique ID of the tuple object

Example

In this example, we will use the id() method to get the unique id of the Tuple object -

myTuple = ("david", "steve", "alexa", "dwyer")
res = id(myTuple)
print(res)
Copy after login

Output

140389997162960
Copy after login

When we run it again, the id will be different -

140674820137424
Copy after login

Unique ID of integer

Example

In this example, we will get the unique ID of the integer -

print(id(50))
print(id(100))
Copy after login

Output

140184574995904
140184574997504
Copy after login

The above is the detailed content of Why does the result of id() not seem to be unique in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template