isinstance()

isinstance() 函数用于检查一个对象是否是特定类的实例,或者是否是从特定类继承而来的类的实例。它支持多态性,即一个对象可以是多个类的实例,特别是当这些类形成一个继承链时。

语法

isinstance(object, classinfo)

参数

object: 要检查的对象。

classinfo: 类的信息。可以是一个类,也可以是一个包含多个类的元组。

返回值

如果对象是classinfo指定的类的实例,或者从这些类继承而来,则返回True;否则返回False。

示例


  1. class Parent:

  2. pass

  3. class Child(Parent):

  4. pass

  5. class GrandChild(Child):

  6. pass

  7. parent = Parent()

  8. child = Child()

  9. grand_child = GrandChild()

  10. print(isinstance(parent, Parent)) # 输出: True

  11. print(isinstance(child, Parent)) # 输出: True

  12. print(isinstance(grand_child, Parent)) # 输出: True

  13. print(isinstance(grand_child, Child)) # 输出: True

  14. print(isinstance(grand_child, GrandChild)) # 输出: True

  15. print(isinstance(grand_child, (Parent, Child))) # 输出: True

type()

type() 函数用于获取一个对象的确切类型。它返回的是对象的类,而不是一个布尔值。

语法

type(object)

参数

object: 要检查的对象。

返回值

返回对象的类型。

示例


  1. class Parent:

  2. pass

  3. class Child(Parent):

  4. pass

  5. class GrandChild(Child):

  6. pass

  7. parent = Parent()

  8. child = Child()

  9. grand_child = GrandChild()

  10. print(type(parent)) # 输出:

  11. print(type(child)) # 输出:

  12. print(type(grand_child)) # 输出:

区别总结

用途:

isinstance() 用来检查对象是否是特定类或其子类的实例。

type() 用来获取对象的确切类型(类)。

多态性支持:

isinstance() 支持多态性,可以检查对象是否是从多个类继承的实例。

type() 只返回对象的具体类型,不考虑继承关系。

返回值:

isinstance() 返回布尔值,表明对象是否为指定类的实例。

type() 返回对象的类型本身。

使用场景:

当你需要检查一个对象是否符合某种类型(可能是基类或接口),通常使用 isinstance()。

当你需要知道对象的确切类型(例如,在某些情况下需要区分 int 和 float),则使用 type()。

比较使用 isinstance() 和 type()


  1. class A:

  2. pass

  3. class B(A):

  4. pass

  5. a = A()

  6. b = B()

  7. print(isinstance(a, A)) # 输出: True

  8. print(isinstance(b, A)) # 输出: True

  9. print(isinstance(b, B)) # 输出: True

  10. print(type(a) == A) # 输出: True

  11. print(type(b) == A) # 输出: False

  12. print(type(b) == B) # 输出: True

在这个例子中,isinstance() 检查 b 是否是 A 的实例,结果为 True,因为 B 继承自 A。而 type(b) == A 的结果为 False,因为 b 的具体类型是 B,而不是 A。

总之,isinstance() 更适用于类型检查,而 type() 更适合于确定对象的具体类型。在实际编程中,通常推荐使用 isinstance() 来检查对象是否符合某种类型或接口,因为它支持多态性,并且更加灵活。

进阶示例

基本类型检查

在这个示例中,我们将展示如何使用 isinstance() 和 type() 来检查基本的数据类型。


  1. # 定义一些基本类型的数据

  2. integer_value = 42

  3. float_value = 3.14

  4. string_value = "Hello, world!"

  5. list_value = [1, 2, 3]

  6. tuple_value = (1, 2, 3)

  7. dict_value = {"key": "value"}

  8. # 使用 isinstance 检查类型

  9. print(isinstance(integer_value, int)) # 输出: True

  10. print(isinstance(float_value, float)) # 输出: True

  11. print(isinstance(string_value, str)) # 输出: True

  12. print(isinstance(list_value, list)) # 输出: True

  13. print(isinstance(tuple_value, tuple)) # 输出: True

  14. print(isinstance(dict_value, dict)) # 输出: True

  15. # 使用 type 检查类型

  16. print(type(integer_value) == int) # 输出: True

  17. print(type(float_value) == float) # 输出: True

  18. print(type(string_value) == str) # 输出: True

  19. print(type(list_value) == list) # 输出: True

  20. print(type(tuple_value) == tuple) # 输出: True

  21. print(type(dict_value) == dict) # 输出: True

在这个例子中,我们使用 isinstance() 和 type() 分别检查了基本类型的数据。isinstance() 返回布尔值,而 type() 返回具体的类型。

继承关系中的类型检查

在这个示例中,我们将展示如何在继承关系中使用 isinstance() 和 type() 来检查对象的类型。


  1. class Base:

  2. pass

  3. class Derived(Base):

  4. pass

  5. # 创建实例

  6. base_instance = Base()

  7. derived_instance = Derived()

  8. # 使用 isinstance 检查类型

  9. print(isinstance(base_instance, Base)) # 输出: True

  10. print(isinstance(derived_instance, Base)) # 输出: True

  11. print(isinstance(derived_instance, Derived)) # 输出: True

  12. # 使用 type 检查类型

  13. print(type(base_instance) == Base) # 输出: True

  14. print(type(derived_instance) == Base) # 输出: False

  15. print(type(derived_instance) == Derived) # 输出: True

在这个例子中,我们定义了两个类 Base 和 Derived,其中 Derived 继承自 Base。使用 isinstance() 检查时,derived_instance 同时是 Base 和 Derived 类型的实例,而使用 type() 时只能检测到其确切类型是 Derived。

检查多重继承

在这个示例中,我们将展示如何使用 isinstance() 和 type() 来检查多重继承的情况。


  1. class A:

  2. pass

  3. class B:

  4. pass

  5. class C(A, B):

  6. pass

  7. # 创建实例

  8. c_instance = C()

  9. # 使用 isinstance 检查类型

  10. print(isinstance(c_instance, A)) # 输出: True

  11. print(isinstance(c_instance, B)) # 输出: True

  12. print(isinstance(c_instance, C)) # 输出: True

  13. # 使用 type 检查类型

  14. print(type(c_instance) == A) # 输出: False

  15. print(type(c_instance) == B) # 输出: False

  16. print(type(c_instance) == C) # 输出: True

在这个例子中,C 类继承自 A 和 B 类。使用 isinstance() 检查时,c_instance 同时是 A、B 和 C 类型的实例,而使用 type() 时只能检测到其确切类型是 C。

使用类名检查类型

在这个示例中,我们将展示如何使用类名来检查类型。


  1. class MyClass:

  2. pass

  3. # 创建实例

  4. instance = MyClass()

  5. # 使用 isinstance 检查类型

  6. print(isinstance(instance, MyClass)) # 输出: True

  7. # 使用 type 检查类型

  8. print(type(instance) == MyClass) # 输出: True

  9. # 使用类名字符串检查类型

  10. print(instance.__class__.__name__ == "MyClass") # 输出: True

在这个例子中,我们使用了类名来检查类型。isinstance() 和 type() 都可以用来检查对象是否是 MyClass 的实例。另外,我们还展示了如何使用 __class__.__name__ 属性来获取对象的类名。

检查类型为元组的情况

在这个示例中,我们将展示如何使用 isinstance() 来检查对象是否为多个类型的实例之一。


  1. # 创建不同类型的数据

  2. integer_value = 42

  3. float_value = 3.14

  4. string_value = "Hello, world!"

  5. list_value = [1, 2, 3]

  6. # 使用 isinstance 检查类型

  7. print(isinstance(integer_value, (int, float))) # 输出: True

  8. print(isinstance(float_value, (int, float))) # 输出: True

  9. print(isinstance(string_value, (int, float))) # 输出: False

  10. print(isinstance(list_value, (int, float))) # 输出: False

  11. # 使用 type 检查类型

  12. print(type(integer_value) in (int, float)) # 输出: True

  13. print(type(float_value) in (int, float)) # 输出: True

  14. print(type(string_value) in (int, float)) # 输出: False

  15. print(type(list_value) in (int, float)) # 输出: False

在这个例子中,我们使用 isinstance() 来检查对象是否是 int 或 float 类型的实例之一。使用 type() 时,我们需要检查对象的类型是否在给定的元组中。

使用 isinstance() 检查模块类型

在这个示例中,我们将展示如何使用 isinstance() 来检查模块类型。


  1. import math

  2. # 使用 isinstance 检查模块类型

  3. print(isinstance(math, module)) # 错误写法,应该使用 __main__.module

  4. # 正确的做法是检查模块的名字

  5. print(math.__name__ == "math") # 输出: True

在这个例子中,我们尝试使用 isinstance() 来检查模块类型,但是由于 module 不是一个内置类型,所以我们需要检查模块的名字。

总结

通过这些示例,我们可以看到 isinstance() 和 type() 在类型检查方面的不同之处:

isinstance() 更适用于检查对象是否为特定类或其子类的实例,支持多态性。

type() 用于获取对象的确切类型,不考虑继承关系。

在实际编程中,通常推荐使用 isinstance() 来检查对象是否符合某种类型或接口,因为它支持多态性,并且更加灵活。而在需要确定对象的确切类型时,可以使用 type()。

最后作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些软件测试的学习资源,希望能给你前进的路上带来帮助。

视频文档获取方式:
这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方小卡片即可自行领取。

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐