Why is it recommended to use isinstance instead of type for instance checking in python?
阿神2017-05-24 11:35:09
0
2
887
http://codingpy.com/article/p...
Example check:
Use isinstance(a, C) instead of type(a) is C`. But generally avoid doing instance checking. It is recommended to check the characteristics of the instance.
When the comparison object is a subclass instance and a parent class,
isinstance
会认为相等,type
is not equal.is equivalent to
isinstance
比type
多了个检查继承的功能,所以更愿意选择isinstance
.