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

相关文章
|
JSON 数据格式
成功解决TypeError: Object of type 'ndarray' is not JSON serializable
成功解决TypeError: Object of type 'ndarray' is not JSON serializable
|
6月前
|
Python
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
|
JSON 数据格式
TypeError: Object of type ‘float32‘ is not JSON serializable
TypeError: Object of type ‘float32‘ is not JSON serializable
215 0
|
Python
AttributeError: 'dict' object has no attribute 'has_key'
AttributeError: 'dict' object has no attribute 'has_key'
153 0
解决AttributeError: ‘model‘ object has no attribute ‘copy‘
原因是没有使用model.state_dict()进行模型保存,但是使用了model.load_state_dict()加载模型。这两者要配套使用。
450 0
AttributeError: ‘list‘ object has no attribute ‘ndim‘
AttributeError: ‘list‘ object has no attribute ‘ndim‘
193 0
|
PyTorch TensorFlow 算法框架/工具
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
277 0
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
|
Python
AttributeError: 'list' object has no attribute 'ndim'
AttributeError: 'list' object has no attribute 'ndim'
541 0
成功解决AttributeError: ‘dict_values‘ object has no attribute ‘index‘
成功解决AttributeError: ‘dict_values‘ object has no attribute ‘index‘