PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Easy
Question 32
Which function should you use to return the identity of an object in Python?
-
A
isinstance() -
B
type() -
C
id() -
D
bool()
Reveal correct answer
Correct answer: C
A.
isinstance() is a function used to check if an object is an instance of a particular class or any of its subclasses. It does not return the identity of the object.
B.
type() is a function used to determine the type of an object. It returns the type of the object, which is a class object, not the identity of the object.
C.
In Python, the id() function is used to return the identity of an object. The identity of an object is a unique integer that represents the object itself. This integer is guaranteed to be unique and constant for the lifetime of the object. The id() function takes an object as its parameter and returns the identity value. This value can be used to compare whether two objects are the same or different.
D.
bool() is a function used to convert a value to a Boolean (True or False) based on its truthiness. It does not return the identity of the object.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
