Type and isinstance in Python – GeeksforGeeks

In this article, we’ll cover the type() and isinstance() function in Python

, and what are the differences between type() and isinstance().

What is type in Python

? Python

has a built-in method called type that is usually useful when figuring out the type of variable used in the program at runtime. The canonical way to check type in Python is given below

:Type() function syntax type(object) type(name, bases, dict)Example

1: Example of type() with a unique object parameter

In this example, we are trying to verify the data type of each variable, such as x, s, and y using the type() function.

Output

:class ‘int’ class ‘str’ class ‘list’Example

2: Example of type() with a name, bases, and dict Parameter

If you need to check the type of an object, it is recommended to use the python isinstance() function instead. This is because the isinstance() function also checks whether the given object is an instance of the subclass.

Output

:{‘b’: 12, ‘a’: ‘Foo’, ‘__dict__’: , ‘__doc__’: None, ‘__weakref__’: } {‘b’: 12, ‘a’: ‘Foo’, ‘__doc__’: None}What is isinstance()

in Python?

The isinstance() function

checks whether the object (first argument) is an instance or subclass of the info class (second argument).

isinstance() function

syntax Syntax: isinstance(object, classinfo)

Parameter

:object : object

  • To check
  • classinfo

: class, type or tuple of classes and types

Return: true if the object is an instance or subclass

of a class, or any element of the false tuple otherwise.

If the class information is not a type or type tuple, a TypeError exception is thrown.

Example

1

: In this example, we will see test isinstance() for the class object.

output

: True False True Example 2:In this example, we will see test isinstance() for the integer object, float and string.

Output

:is a float: True is an integer: True is a string:

True Example

3:

In this example, we will see test isinstance() for

the

tuple, the list, The dictionary and the Set object.

Output

:is a tuple: True is a set: True is a list: True is a dict: TrueWhat are the differences between type() and isinstance()?

An elementary mistake people make is to use the type() function where isinstance() would be most appropriate.

If you are checking whether an object has a particular type, you want

isinstance

  • () because it checks whether the object passed in the first argument is of the type of any of the objects of type passed in the second argument. Therefore, it works as expected with old-style subclasses and classes, all of which have the inherited type object instance.
  • type(), on the other hand, simply returns the type object of an object, and comparing what it returns to another type object will only produce True when you use the exact same type object on both sides. In Python, it is preferable to use Duck Typing (type checking is deferred to runtime and implemented by dynamic writing or reflection) rather than inspecting the object type.

Output

:Geeksforgeeks Fox Bear The

  • next reason for not using type() is the lack of support for inheritance

.

output:

False True True False

  • The MyDict class has all the properties of a dictation, without any new methods. It will behave exactly like a dictionary. But type() will not return the expected result. Using isinstance() is preferable in this case because it will give the expected result

:

Output:

True True False True

Contact US