I'm just going through some python help documents and came across the following piece of code :
isinstance(object, type)
Can anyone explain what does type
mean in the above statement?
Thanks,
Vineel
type
must be an object denoting a type/class, such as int
or str
. E.g., isinstance(1, int)
evaluates to True
, while isinstance(sys.stdin, str)
evaluates to False
. If you've defined a class Foo
, then Foo
is also a type object.
Edit: as @delnan notes, type
itself is also a type in Python, so isinstance(str, type)
is true because str
is a type, while isinstance('foo', type)
is false. object
is also a type in Python, and is the root of the type hierarchy.
isinstance(object, classinfo)
object
- object to be checkedclassinfo
- class, type, or tuple of classes and types
The isinstance()
returns:True
if the object is an instance or subclass of a class, or any element of the tupleFalse
otherwise
eg: a = 1 + 2j
print(isinstance(1+2j, complex))
output : True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With