Python isinstance() Function

https://www.w3schools.com/python/ref_func_isinstance.asp

Example

Check if the number 5 is an integer:

x = isinstance(5, int)
 

Definition and Usage

The isinstance() function returns True if the specified object is of the specified type, otherwise False.

If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.


Syntax

isinstance(objecttype)

Parameter Values

ParameterDescription
object Required. An object.
type A type or a class, or a tuple of types and/or classes

More Examples

Example

Check if "Hello" is one of the types described in the type parameter:

x = isinstance("Hello", (float, int, str, list, dict, tuple))
 

xample

Check if y is an instance of myObj:

class myObj:
  name = "John"

y = myObj()

x = isinstance(y, myObj)
posted on 2022-09-22 14:56  guolongnv  阅读(62)  评论(0)    收藏  举报