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
WBOYforward
2023-09-03 16:49:11879browse

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)

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)

Output

140571958913920

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

140597372271552

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)

Output

140389997162960

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

140674820137424

Unique ID of integer

Example

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

print(id(50))
print(id(100))

Output

140184574995904
140184574997504

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete