isinstance 和 type 的区别

简介: isinstance 和 type 的区别。

isinstance 和 type 的区别在于:

type()不会认为子类是一种父类类型。
isinstance()会认为子类是一种父类类型。

class A:
... pass
...
class B(A):
... pass
...
isinstance(A(), A)
True
type(A()) == A
True
isinstance(B(), A)
True
type(B()) == A
False

相关文章
|
7月前
|
Python
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
|
8月前
|
机器学习/深度学习 监控 数据可视化
【已解决】 ‘Conv2d’ object has no attribute ‘register_full_backward_hook’
【已解决】 ‘Conv2d’ object has no attribute ‘register_full_backward_hook’
YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法
YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法
738 0
|
Python
AttributeError: 'dict' object has no attribute 'has_key'
AttributeError: 'dict' object has no attribute 'has_key'
173 0
解决AttributeError: ‘model‘ object has no attribute ‘copy‘
原因是没有使用model.state_dict()进行模型保存,但是使用了model.load_state_dict()加载模型。这两者要配套使用。
510 0
AttributeError: ‘list‘ object has no attribute ‘ndim‘
AttributeError: ‘list‘ object has no attribute ‘ndim‘
199 0
|
TensorFlow 算法框架/工具
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
291 0
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
|
Python
AttributeError: 'list' object has no attribute 'ndim'
AttributeError: 'list' object has no attribute 'ndim'
548 0
成功解决AttributeError: ‘dict_values‘ object has no attribute ‘index‘
成功解决AttributeError: ‘dict_values‘ object has no attribute ‘index‘