isinstance Respects Inheritance, type() Does Not

Medium ⏱ 12 min 62% acceptance ★★★★☆ 4.3
Given class Animal and class Dog(Animal), write classify(obj) returning a tuple of two booleans: (isinstance(obj, Animal), type(obj) is Animal). For a Dog instance that is (True, False) — the interview point being that type() ignores subclassing while isinstance honors it.

Examples

Example 1
Input
classify(Dog())
Output
(True, False)
Explanation

A Dog is-an Animal by isinstance, but its exact type is Dog.

Constraints

  • Return exactly that tuple.
  • Define both classes yourself.

Topics

OOPisinstance

Companies

MicrosoftIBMOracle

Hints

Hint 1

type(obj) is Animal is an exact-class test.

Hint 2

Prefer isinstance in real code; know both for interviews.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.