Difference between isinstance and type in Python

Introduction

Python programming language is a dynamically typed language. In other words, variable types are not explicitly declared as in statically typed languages (ex. C++). Instead, type checking is performed at runtime as opposed to compile time. The debate of which is better? a dynamically typed or statically typed language is not discussed in this post.

In the example below, x is not declared as integer nor y is declared as string, however Python interpreter recognizes variable types from the assigned values.

Dynamic typing is convenient but it may cause some operations to fail due to incompatible variable types. For example, you cannot add an integer to a string. Consider the following example:

If we run the code snippet above, we will get a type error as follows:

type() vs isinstance()

To prevent such errors by sanitizing input, Python provides builtin functions to perform type checking at runtime. In today’s code snippet, we are going to give examples for the following functions:

  1. type(object) : as the name indicates, it returns the type definition of the input object
  2. isinstance(object, class) : returns true if the object is an instance of the provided class or an instance of a derived class (i.e. sub class)

Let us take few examples…

If we run the code snippet above, we will get the following output:

Which function to use?

Both functions type() and isinstance() can be used to get the type of an object but It is recommended to use isinstace() as it supports inheritance in addition to type checking. On the other hand, type() is conveniently available if only type checking is needed. Since Python is dynamically typed language, a question arises: do we really need type checking in the first place? This brings us to duck typing…

Duck typing

If you are curious about the origin of the name? it comes from the phrase “If it looks like a duck and quacks like a duck, it’s a duck” but what does that mean ? The idea is simple, since Python is dynamically typed language (i.e. variable types are not declared) then there is no need to specify object types in order to know if an operation is valid or not. We just invoke the operation assuming it will work just fine if it is defined otherwise it will fail. We can always include duck typed code in a try catch block and handle type exceptions in case they occur. In other words, explicitly declaring object types is not needed before performing any operation on an object as it is only a matter of concern at runtime. Duck typing is nothing but a side effect of the language design, it is not intentional.

Here is an example:

If we run the code snippet above, we will get the following output:

As you can see, Python interpreter recognized the integers so addition was performed. Strings and lists were also recognized so concatenation was performed.

That is it for today. Let us summarize…

Summary
type(object) returns the type definition of the input object
isinstance(object, class) returns true if the provided object is an instance of the provided class or a derived class
Duck typing: explicitly declaring object types is not needed before performing any operation on an object

References

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *