Isinstance () function in Python
The built-in isinstance () function in Python checks whether an object is an instance or a subclass of Classinfo.
The built-in isinstance () function checks whether an object (first parameter) is an instance or a subclass of Classinfo (the second parameter).
The isinstance () function syntax in Python
isinstance(object, classinfo)
Parameters of isinstance function ()
The isinstance () function has 2 parameters:
object: object to checkclassinfo: class, type, or tuple
Value returned from isinstance ()
Isinstance () function returns:
Trueif the object is an instance or a subclass of class or any element of the data set.Falseif vice versa.
For example, if the classinfo parameter is a tuple, this function will return True if the object is a type in tuple.
Example: How does isinstance () function work?
class Foo: a = 5 fooInstance = Foo() print(isinstance(fooInstance, Foo)) print(isinstance(fooInstance, (list, tuple))) print(isinstance(fooInstance, (list, tuple, Foo)))
Run the program, the result is:
True False True
See also: Built-in Python functions
You've just finished reading the article "Isinstance () function in Python" edited by the TipsMake team. You can save isinstance-function-in-python.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.
- Zip () function in Python
- Int () function in Python
- The function id () in Python
- The oct () function in Python
- The next () function in Python
- Hex () function in Python
- The map () function in Python
- The function dir () in Python
- The function set () in Python
- Help () function in Python
- Sum () function in Python
- The slice () function in Python
- The chr () function in Python
- The pow () function in Python