Difference between isinstance and type in python -


this question has answer here:

i looking @ code on web, , saw code i'm not used to. 1 called attention was:

if not isinstance(string, str):     #dosomething 

what difference if did instead:

if type(string)!=str:     #dosomething 

first check out great answers here.

type() returns type of object. whereas, isinstance():

returns true if object argument instance of classinfo argument, or of (direct, indirect or virtual) subclass thereof.

example:

class mystring(str):     pass  my_str = mystring() if type(my_str) == 'str':     print 'i hope prints' else:     print 'cannot check subclasses' if isinstance(my_str, str):     print 'definitely prints' 

prints:

cannot check subclasses prints 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -