Basic usage of python built-in function isinstance, pythonisinstance
Syntax: isinstance (object, type)Purpose: To determine whether an object is of a known type.The first parameter (object) is the object, and the second parameter (type) is the type name (int ...) or a list of type names (int, list, float) is a list ).The return value is Boolean (True or flase ).Returns True if the object type is the same as that of parameter 2. If the two parameters are a single tuples, True is returned if the object type is the same as one of the type names in the tuples.Note: If you do not know the type, you can use type to view the type of the character.
Example: a = 10
Print (type ())
Printed result: <class 'int'>
Below are several examples to help us understand:
Example 1:
Abc = 1 # judge whether it is of the int type
Print (isinstance (abc, int ))
--> Return value True
Print (isinstance (abc, (int, str, float )))
--> Return value True
Print (isinstance (abc, (str, float )))
--> Return value False
Example 2:
Abcd = "1" # determine if it is of the str type
Print (isinstance (abcd, int ))
--> Return value False
Print (isinstance (abcd, (int, str, float )))
--> Return value True
Print (isinstance (abcd, (str, float )))
--> Return value True
Example 3:
Abced = [1, 2, 3] # determine if it is a list
Print (isinstance (abced, (list )))
--> Return value True
Abcdef = {"a": 5, "B": 6} # determine whether the dictionary is used
Print (isinstance (abcdef, (dict )))
--> Return value True
Abcdefg = (, 3) # determine whether it is a tuples
Print (isinstance (abcdefg, (tuple )))
--> Return value True