Python id()
is a built-in function that returns the identity of an object. That returned identity is unique and constant throughout the lifetime of that object.
id(object)
Python id()
function only takes one parameter.
The returned value identity which is an integer is unique and constant through the lifetime of that object.
Note: In CPython implementation, the returned identity is the address of the object in memory.
Remember in Python everything is treated as an object, be it function, class or any variable.
>>> def func(x):
return x
>>> id(func)
46568696
>>> str1 = 'YOLO'
>>> id(str1)
46491776
>>> x = (1,2,3)
>>> id(x)
46543856
>>> class Example:
def foo(a):
return a
>>> obj = Example()
>>> id(obj)
41994224
As you can see in above example, for any object id()
function returns a unique and constant identity that is throughout the lifetime of that object.