Skip to content

Check is instance

Python is not a strong-typed language. It is true that it support type hints, but it is not enforced. However, sometimes it is useful to check if a variable is of a certain type.

src.beginner.check_is_instance

Module to check if an instance is of a given type.

This module contains a function to check if an instance is of a given type.

src.beginner.check_is_instance.is_instance(instance, instance_type)

Check if instance is of the type provided.

Parameters:

Name Type Description Default
instance object

any kind of object.

required
instance_type class or tuple

expected instance type for instance.

required

Returns:

Name Type Description
bool bool

true if instance is the one expected, false otherwise.

Source code in src/beginner/check_is_instance/check_is_instance.py
 8
 9
10
11
12
13
14
15
16
17
18
def is_instance(instance, instance_type) -> bool:
    """Check if instance is of the type provided.

    Parameters:
        instance (object): any kind of object.
        instance_type (class or tuple): expected instance type for instance.

    Returns:
        bool: true if instance is the one expected, false otherwise.
    """
    return isinstance(instance, instance_type)