Python判断数据类型

Python判断数据类型

方法

  1. type
  2. isinstance

区别

  1. type:不会认为子类是父类

  2. isinstance:会认为子类是父类类型

class Color(object):
    pass
 
class Red(Color):
    pass
 
print type(Color()) == Color  # True
print type(Red()) == Color  # False
print isinstance(Red(),Color)  # True

常用类型

数据类型 判断类型
字符串 str
整数 int
浮点数 float
时间类型 datetime.datetime
布尔类型 bool
字典类型 dict
列表 list

你可能感兴趣的:(Python)