Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python3
python
communitycreator

What is the id() function in Python?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Python, the id() function is used to return an object’s identity.

Syntax

id(object)

Parameter

  • object: This represents the identity to be returned.

Return value

This function returns an object’s identity.

Example

# create a class with a property
class Person:
name = "Maria"
# create object of the class
person = Person()
# get the id
print('Id:',id(person))
# create a variable
x = 10
print('Id:',id(x))
# create a list
my_list = [12, 3.5, "shot"]
print('Id:',id(my_list))

Explanation

  • Lines 2 and 3: We create a class named Person with a property named name.
  • Line 6: We create an object of the class named person.
  • Line 8: We display the identity of person using id().
  • Lines 11 and 12: We create a variable x and display its identity.
  • Lines 15 and 16: We create a list my_list and display its identity.

RELATED TAGS

python3
python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring