type() and isinstance() in Python with Examples

Posted in /  

type() and isinstance() in Python with Examples
vinaykhatri

Vinay Khatri
Last updated on October 10, 2024

    Python has many built-in functions. Using those functions, we can get a quick result. Out of many Python functions, type() and instance() are the two most commonly used methods. In python, everything is an object, so here we interchangeably use the terms methods and functions.

    What is type() in Python?

    The Python type() function is generally used to output the data type of the object. In Python, everything is an object. Using the type() method, we can grab the actual class of that object.

    Python type() syntax:

    The type() function can either accept 1 parameter, or 3 parameters

    type(object)
    
    type(name, base, dict)

    The type() function with single Parameter

    If we pass a single parameter object to type() function, it will return the data type of the object.

    Example

    number = 24
    decimal_number = 25.78
    string = "2543"
    
    class MyClass():
        pass
    
    myObj = MyClass()
    
    print("The type of number is: ", type(number))
    print("The type of decimal_number is: ", type(decimal_number))
    print("The type of string  is: ", type(string))
    print("The type of MyClass is: ", type(myObj))

    Output

    The type of number is:  <class 'int'>
    The type of decimal_number is:  <class 'float'>
    The type of string  is:  <class 'str'>
    The type of MyClass is:  <class '__main__.MyClass'>

    From the above example, you can see that if we pass a single object to the type() function, it returns its main class or data type. In Python, everything is defined under a class. That’s why we get such output < class ‘ str’> , < class ‘int’> , or < class ‘__main__.CustomClass_name> .

    Python type(name, base, dict) function with three parameters

    Using the type() method, we can also define a new type. Usually, we define a custom or user-defined class to define a new type, but using the type method, we can perform a similar function.

    Syntax

    type(name, base, dict)
    name It is a string that defines the name or type of the class.
    base It is a tuple that itemizes the base class.
    dict It is a dictionary that contains class properties.

    Example

    class MyClass:
        age = 20
    
    my_obj = MyClass()
    print("Type of Custom Class Object: ",type(my_obj))
    
    #the above code is equivalent to
    
    my_obj1 = type("MyClass1", (object,), dict(age=21))
    print("Type of my_obj1", type(my_obj))
    print("String representation of my_obj1:",my_obj1)

    Output

    Type of Custom Class Object:  <class '__main__.MyClass'>
    Type of my_obj1 <class '__main__.MyClass'>
    String representation of my_obj1: <class '__main__.MyClass1'>

    What is isinstance() in Python?

    The isinstance() is an inbuilt Python function. It accepts two parameters object and classtype, and return True if the object type is similar to the classtype; else, it returns False. It is generally used to check if the object data type is of a specific data type or not.

    Python isinstance () syntax

    isinstance(object, classtype) The object parameter is the instance, variable, or value which type we are comparing. The classtype is a class or type to which we are comparing the object data type. The isinstance() function returns a Boolean data type True or False

    Python isinstace() function examples

    Python integer check Example with isinstance:

    number = 234
    decimal_number = 234.3
    
    print("Is number is an instance of int:", isinstance(number, int))
    print("Is decimal_number is an instance of int: ", isinstance(decimal_number, int) )

    Output

    Is number is an instance of int: True
    Is decimal_number is an instance of int:  False

    Python string check Example with isinstance()

    string = "234"
    number= 234.3
    
    print("Is string is an instance of str:", isinstance(string, str))
    print("Is number is an instance of str: ", isinstance(number, str) )

    Output

    Is string is an instance of str: True
    Is number is an instance of str:  False

    Python class instance check Example with isinstance()

    class Men:
        age = 20
    
    class Women:
        age =18
    
    rahul = Men()
    ram = Men()
    
    print("Is ram  is an instance of Men:", isinstance(ram, Men))
    print("Is ram  is an instance of Women:", isinstance(ram, Women))
    print("are  ram  and rahul instances of Men :", isinstance(ram, Men) == isinstance(rahul, Men))

    Output

    Is ram an instance of Men: True
    Is ram an instance of Women: False
    are ram and rahul instances of Men : True

    Difference Between Python type() and isinstance() functions

    Python type()

    Python isinstance()

    The Python type() function is used to determine the data type or class of an object. It can also be used to define a new class type. The Python isinstance() function is used to check if the object has a specific data type.
    It returns the type of the object. It returns a Boolean Value, by checking if the object has a specific data type.

    Python type() Example Vs isinstance() example difference #Type()

    class Human:
       living ="yes"
    
    class Men(Human):
        mortal = "No"
    
    #human type is <class '__main__.Human'>
    print("Is Type of Human() is equal to Human:",type(Human())== Human)  
    
    #Men type is <class '__main__.Men'>
    print("Is type of Men() is equal to Human:",type(Men()) == Human)

    Output

    Is Type of Human() is equal to Human: True
    Is type of Men() is equal to Human: False

    #isinstance()

    class Human:
       living ="yes"
    
    class Men(Human):
        mortal = "No"
    
    print("Is Human() is an instance of Human: ",isinstance(Human(), Human))
    # True because of Inheritance
    print("Is Men() is an instance of Human:",isinstance(Men(), Human))

    Output

    Is Human() is an instance of Human:  True
    Is Men() is an instance of Human True

    Summary

    • Both type() and isinstance() are the built-in Python functions
    • The type method can accept 1 or 3 parameters.
    • With 1 argument type(object), the function returns the data type of the object.
    • With 3 arguments type(name, objects, dict), the function returns a new class
    • The isinstance(), check if the object is of a specific data type or class.
    • The isinstance returns a boolean value.

    People are also reading:

    Leave a Comment on this Post

    0 Comments