Causes and remedies when the following error occurs in is instance.
TypeError: isinstance() arg 2 must be a type or tuple of types
Error that the second argument is not of type. Occurs even though the type (list) is set correctly.
python
arr = [1,2,3]
#"List" is specified in the second argument
if isinstance(arr, list):
print ("YES")
#TypeError: isinstance() arg 2 must be a type or tuple of types
Because I set a variable called list before this code. It was a list = variable instead of a list = type.
There are two ways to deal with it. ① Do not use a variable with the same name as the type. (2) Specify the argument as type (type name).
The basics are ①.
▼ ② When the argument is specified as type (type name)
python
arr = [1,2,3]
if isinstance(arr, type(list)):
print ("YES")
#YES
Processing completed normally.
Recommended Posts