Difference between isinstance and type in python
This question already has an answer here:
First check out all the great answers here.
type() simply returns the type of an object. Whereas, isinstance():
Returns true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof.
Example:
class MyString(str):
pass
my_str = MyString()
if type(my_str) == 'str':
print 'I hope this prints'
else:
print 'cannot check subclasses'
if isinstance(my_str, str):
print 'definitely prints'
Prints:
cannot check subclasses
definitely prints
链接地址: http://www.djcxy.com/p/54222.html
上一篇: 检查变量是否为列表的最佳方法是什么?