How to determine an object's class?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Determining an object's class depends on the language and on what you actually need to know. Sometimes you need the exact runtime class. Sometimes you only need to know whether the object is an instance of a broader base type or interface. Those are related, but not identical, questions.
Python
In Python, the most common tools are type() and isinstance():
type(obj) gives the exact runtime class. isinstance(obj, Fruit) asks whether the object belongs to that class or one of its subclasses. In most real code, isinstance() is safer because it respects inheritance.
Java
In Java, getClass() returns the runtime class:
If you want to test whether an object belongs to a type, use instanceof:
As in Python, exact class lookup and "is this compatible with type X" are different questions.
JavaScript
JavaScript is more prototype-based, but common checks still exist:
constructor.name can be useful for debugging, but instanceof is usually the better semantic test when you care about behavior rather than a printable class label.
Why Exact Class Checks Can Be Risky
Exact class equality is sometimes too strict. If a subclass should be accepted, checking the exact class can reject valid objects. That is why many object-oriented codebases prefer "is this an instance of the required abstraction" over "is this exactly class X."
This matters especially in frameworks, testing, and polymorphic designs where subtype substitution is expected.
When Exact Class Is Useful
Exact class lookup still has valid uses:
- debugging runtime behavior
- logging diagnostics
- serialization frameworks
- reflection-heavy code
- cases where subclass behavior must be excluded deliberately
So the best tool depends on intent, not just language syntax.
That is the main design lesson here. Type-introspection code is often a sign that the program is making a behavior decision at runtime, so it is worth asking whether polymorphism or an interface check would express the intent more cleanly than exact class inspection.
Good object-oriented code often needs less exact class probing than beginners expect.
If you keep finding yourself switching on exact class names, that can be a design smell. It often means behavior should live on the objects themselves, or that an interface-based dispatch mechanism would be cleaner and more extensible than repeated manual type checks.
That design perspective is often more valuable than the syntax itself.
Common Pitfalls
- Using exact class equality when inheritance should be allowed.
- Using class-name strings for logic instead of real type checks.
- Assuming all languages treat runtime type information the same way.
- Confusing constructor metadata with instance compatibility checks.
- Writing brittle code that breaks as soon as a subclass appears.
Summary
- Most languages offer one tool for exact runtime class lookup and another for instance compatibility.
- In Python,
type()andisinstance()answer different questions. - In Java, use
getClass()for exact type andinstanceoffor compatibility. - In JavaScript,
constructor.nameis informative, butinstanceofis often the real check. - Choose the check that matches your intent rather than the one that merely looks most direct.
Related reading
- How to determine an overfitted model based on loss precision and recall
- How to determine an overfitted model based on loss precision and recall
- How to determine column to be Quantitative or Categorical data?
- How to determine the learning rate and the variance in a gradient descent algorithm?
- How to determine the number of layers and nodes of a neural network
- How to determine what type of layers do I need for my Deep learning model?
- How to disable dropout while prediction in keras?
- How to disable GPU in keras with tensorflow?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.