>>> type(a)
<class 'str'>

type() 函数如果你只有第一个参数则返回对象的类型,三个参数返回新的类型对象。

语法:

type(object)
type(name, bases, dict)

参数

  • name -- 类的名称。
  • bases -- 基类的元组。
  • dict -- 字典,类内定义的命名空间变量。

判断数据类型:isinstance()

>>> a = '你好'
>>> isinstance(a,str)
True
>>> isinstance(a,int)
False

isinstance() 与 type() 区别:

  • type() 不会认为子类是一种父类类型,不考虑继承关系。

  • isinstance() 会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同推荐使用 isinstance()。

 

 

相关文章:

  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2021-06-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-18
  • 2022-12-23
  • 2021-04-20
  • 2021-06-29
  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案