您的位置:首页技术文章
文章详情页

为什么python中实例检查推荐使用isinstance而不是type?

浏览:38日期:2022-06-30 11:46:42

问题描述

http://codingpy.com/article/p...

实例检查:

使用isinstance(a, C),而不是type(a) is C`。但是一般要避免做实例检查。建议检查实例的特性。

问题解答

回答1:

当遇到比较对象是子类实例与父类时,isinstance会认为相等,type则不等。

相当于 isinstance 比 type 多了个检查继承的功能,所以更愿意选择 isinstance。

回答2:

/q/1010000000127305看这个,更详细一点

标签: Python 编程
相关文章: