【python日用】python中isinstance()函数

语法

isinstance(object, classinfo)

函数定义

def isinstance(x, A_tuple): # real signature unknown; restored from __doc__
    """
    Return whether an object is an instance of a class or of a subclass thereof.
    
    A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
    check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
    or ...`` etc.
    """
    pass

简单来说呢就是,isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
但是它与type又有区别:

  • type() 不会认为子类是一种父类类型,不考虑继承关系。
  • isinstance() 会认为子类是一种父类类型,考虑继承关系。
    此处的classinfo对于普通类型来说可以是
  • int,
  • float,
  • bool,
  • complex,
  • str(字符串),
  • list,
  • dict(字典),
  • set,
  • tuple

示例

isinstance(2,int)

猜你喜欢

转载自blog.csdn.net/weixin_51656605/article/details/112982950
今日推荐