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
Post a Comment