(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
Python id() function
Updated on Jan 07, 2020
id() function returns a unique numeric identifier associated with the object.
In standard Python (i.e CPython) the identifier represents the memory address of the object. Although, this may vary in other implementation.
The unique identifier spring into existing when you define an object and will not change until the program is running. We can use this identifier to determine whether two objects are same or not.
The syntax of
id() function is as follows:
id(obj) -> unique identifier
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
>>> >>> a = 10 >>> >>> b = 5 >>> >>> >>> id(a), id(b) (10919712, 10919552) >>> >>> >>> a = b # a now references same object as b >>> >>> >>> id(a), id(b) (10919552, 10919552) >>>
a = 10 b = 5 print(id(a), id(b)) a = b # a now references same object as b print(id(a), id(b))
b references two distinct objects. As a result,
id() call returns two unique identifiers. Next, we assign the object
b references the same object (
5). Thus, the next
id() call returns the same identifier.
Other Tutorials (Sponsors)
This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!