Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is the isinstance() method in Python?

Educative Team

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.

The isinstance() method matches the specified object with the specified data type.

  • It returns true if the object belongs to the same data type
  • It returns false if the object belongs to a different data type

You can specify multiple types in the form of a tuple.

Code

Single data type

print("'Educative' is a string: ",
isinstance("Educative", str))

Multiple data types

In the example below, we have specified the int, float, and list as a data type. Since the object is a string, it returns false.

print("'Educative' is a string: ",
isinstance("Educative", (int, float, list)))

User-defined types

We have created the class website and instantiated its object educative in line[4]. Later in this example, we verify its type:

class Website:
domain = 'educative.io'
Educative = Website()
print("Educative is a website: ",
isinstance(Educative, Website))

RELATED TAGS

python
Copyright ©2022 Educative, Inc. All rights reserved

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