Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isinstance(object,type) in python

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

like image 463
vineel Avatar asked Nov 08 '11 14:11

vineel


2 Answers

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.

like image 99
Fred Foo Avatar answered Sep 27 '22 17:09

Fred Foo


isinstance(object, classinfo)
object - object to be checked
classinfo - 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 tuple
False otherwise
eg: a = 1 + 2j

print(isinstance(1+2j, complex))
output : True
like image 21
B.Narendra Avatar answered Sep 27 '22 17:09

B.Narendra