• 通过help命令可以查看内建函数的帮助文档说明,如下:

>>> help(isinstance)
Help on built-in function isinstance in module __builtin__:

isinstance(...)
isinstance(object, class-or-type-or-tuple) -> bool

Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).

  • 上述帮助文档中可以看到,isinstance为内建函数,其用法为:第一个参数为被判断的对象;第二个参数为判断该对象的类型,可以为单一类型也可以以元组的形式来判断该对象的类型,返回结果为一个bool值,为真为true,否则为false。
  • 两个参数的关系:若对象的类型与参数二的类型相同则返回True。若参数二为一个元组,则若对象类型与元组中类型名之一相同即返回True。
  •   示例:

  >>> a = 10

  >>> isinstance(a,int)     #一比一判断参数类型,如果符合要求,那么返回bool型  true
  True

  >>> isinstance(a,str)     #一比一判断参数类型,如果不符合要求,那么返回bool型  false
  False

#一比多判断参数类型,如果符合要求,那么返回bool型  true
  True

相关文章:

  • 2021-09-11
  • 2022-12-23
  • 2022-01-15
  • 2021-07-10
  • 2021-10-08
猜你喜欢
  • 2021-11-15
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
相关资源
相似解决方案