type checking
instance validation
programming
object-oriented
software development

Check if instance is of a type

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Checking whether an instance is of a given type is a basic operation in many languages, but the exact tool depends on the language and on whether inheritance should count. The important distinction is usually not just "what syntax do I use," but "am I checking the exact runtime type or whether the object is compatible with a broader parent type or interface."

Python Uses isinstance

In Python, the standard answer is isinstance.

python
1class Animal:
2    pass
3
4
5class Dog(Animal):
6    pass
7
8
9pet = Dog()
10
11print(isinstance(pet, Dog))     # True
12print(isinstance(pet, Animal))  # True

isinstance returns True for subclasses too, which is what you want most of the time.

Java Uses instanceof

Java uses the instanceof operator.

java
1class Animal {}
2class Dog extends Animal {}
3
4Animal pet = new Dog();
5
6System.out.println(pet instanceof Dog);    // true
7System.out.println(pet instanceof Animal); // true

This is especially useful when dealing with polymorphic values stored under a base type reference.

C# Uses is

In C#, the common operator is is.

csharp
1object value = "hello";
2
3Console.WriteLine(value is string);  // True
4Console.WriteLine(value is int);     // False

Modern C# also lets you combine the type check with pattern matching, which often reads better than checking and casting separately.

csharp
1if (value is string text)
2{
3    Console.WriteLine(text.Length);
4}

JavaScript Is More Context-Dependent

In JavaScript, instanceof works for class and constructor relationships, but not for primitive values in the same way developers from other languages may expect.

javascript
1class Animal {}
2class Dog extends Animal {}
3
4const pet = new Dog();
5
6console.log(pet instanceof Dog);    // true
7console.log(pet instanceof Animal); // true
8console.log("hello" instanceof String); // false

For primitives, typeof is often the correct tool instead.

Exact Type Versus Assignable Type

This is the conceptual trap behind many type-check questions. In most languages:

  • instance compatibility checks include subclasses
  • exact runtime type checks are stricter

For example, in Python type(obj) is SomeClass is stricter than isinstance(obj, SomeClass). Similar distinctions exist in other languages too.

Choose the strict version only when you really need exact-type behavior.

Prefer Polymorphism Over Repeated Type Checks

If your code is full of if chains that ask what type something is, that can be a design smell. In object-oriented code, polymorphism is often a better answer than manual type branching.

Type checks still have legitimate uses, especially at boundaries, deserialization points, or framework integration layers. The goal is not to ban them, but to use them where they clarify behavior instead of compensating for weak design.

Test Interface Compatibility When That Is the Real Requirement

Sometimes the real question is not whether an object is one concrete class, but whether it satisfies an interface or base contract. Checking against the abstraction is usually more robust than checking the most specific type.

Common Pitfalls

  • Confusing exact type checks with inheritance-aware instance checks.
  • Using JavaScript instanceof where typeof is the correct primitive check.
  • Repeating many type checks when polymorphism would simplify the design.
  • Forgetting that interfaces and base classes often count as valid matches.
  • Assuming syntax from one language maps directly to another.

Summary

  • Python uses isinstance, Java uses instanceof, and C# uses is.
  • Most instance checks include subclasses or compatible parent types.
  • Exact runtime type checks are stricter and should be used deliberately.
  • JavaScript needs extra care because instanceof and typeof solve different problems.
  • Type checks are useful, but too many of them can signal a design issue.

Course illustration
Course illustration

All Rights Reserved.