Scripting

Python isinstance() Function

Python isinstance() Function

Introduction

The isinstance() function in Python allows checking if an object is of the specified type. In other words, it will check if the 1st parameter is a subclass of the 2nd parameter.

If it matches it will return True, otherwise, it will return False.

Now we’re gonna teach you to use the isinstance() function in Python.

Example

Example 1:

x = isinstance(1, int)

print(x)

Output:

True

Example 2:

x = isinstance(1, float)

print(x)

Output:

False

Definition

If an object is of the specified type, this function will return True, otherwise, it will return False.

The case the parameter is of type tuple, if the object is one of type tuple, it will return True.

The syntax

isinstance(object, type)

Parameter Values:

object: an object(Required)

type: class, type or tuple

More examples

Example 1:

class NAME:

name = "BEND"

z = NAME()

x = isinstance(z, NAME)

print(x)

Output:

True

Example 2:

testlist = [1, 2, 3]

print(isinstance(testlist, list))

Output:

True

Conclusion

We just taught you to use the isinstance() function in Python.

Thank you for referring!

Similar Posts