Python id() Function

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.

python id() function

Python id() Syntax


Python id() function only takes one parameter.

  • object (required) – the name of the object which identity is to be returned.

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.

Python id() Example

Remember in Python everything is treated as an object, be it function, class or any variable.

>>> def func(x):
       return x
>>> id(func)

>>> str1 = 'YOLO'
>>> id(str1)

>>> x = (1,2,3)
>>> id(x)

>>> class Example:
      def foo(a):
         return a
>>> obj = Example()
>>> id(obj)

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.