【Python】Python—判断变量的基本类型

type()

>>> type(123)==type(456)
True
>>> type(123)==int
True
>>> type('abc')==type('123')
True
>>> type('abc')==str
True
>>> type('abc')==type(123)
False

isinstance()

>>> isinstance('a', str)
True
>>> isinstance(123, int)
True
>>> isinstance(b'a', bytes)
True

dir()

如果要获得一个对象的所有属性和方法,可以使用dir()函数,它返回一个包含字符串的list,比如,获得一个str对象的所有属性和方法:

>>> dir('ABC')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Python在定义变量的时候不用指明具体的的类型,解释器会在运行的时候会自动检查 变量的类型,并根据需要进行隐式的类型转化。因为Python是动态语言,所以一般情 况下是不推荐进行类型转化的。比如"+"操作时,如果加号两边是数据就进行加法操 作,如果两边是字符串就进行字符串连接操作,如果两边是列表就进行合并操作,甚 至可以进行复数的运算。解释器会在运行时根据两边的变量的类型调用不同的内部方法。 当加号两边的变量类型不一样的时候,又不能进行类型转化,就会抛出TypeError的异常。

但是在实际的开发中,为了提高代码的健壮性,我们还是需要进行类型检查的。而进行 类型检查首先想到的就是用type(),比如使用type判断一个int类型。

import types
if type(1) is types.Integer:
    print('1是int类型')
else:
    print('1不是int类型')

上面的程序会输出:1是int类型

我们在types中可以找到一些常用的类型,在2.7.6中显示的结果:

types.BooleanType              #  bool类型
types.BufferType               #  buffer类型
types.BuiltinFunctionType      #  内建函数,比如len()
types.BuiltinMethodType        #  内建方法,指的是类中的方法
types.ClassType                #  类类型
types.CodeType                 #  代码块类型
types.ComplexType              #  复数类型
types.DictProxyType            #  字典代理类型
types.DictType                 #  字典类型
types.DictionaryType           #  字典备用的类型
types.EllipsisType
types.FileType                 #  文件类型
types.FloatType                #  浮点类型
types.FrameType
types.FunctionType             #  函数类型
types.GeneratorType       
types.GetSetDescriptorType
types.InstanceType             #  实例类型
types.IntType                  #  int类型
types.LambdaType               #  lambda类型
types.ListType                 #  列表类型
types.LongType                 #  long类型
types.MemberDescriptorType
types.MethodType               #  方法类型
types.ModuleType               #  module类型
types.NoneType                 #  None类型
types.NotImplementedType
types.ObjectType               #  object类型
types.SliceTypeh
types.StringType               #  字符串类型
types.StringTypes     
types.TracebackType   
types.TupleType                #  元组类型
types.TypeType                 #  类型本身
types.UnboundMethodType
types.UnicodeType    
types.XRangeType

在Python 3中,类型已经明显减少了很多

types.BuiltinFunctionType
types.BuiltinMethodType
types.CodeType
types.DynamicClassAttribute
types.FrameType
types.FunctionType
types.GeneratorType
types.GetSetDescriptorType
types.LambdaType
types.MappingProxyType
types.MemberDescriptorType
types.MethodType
types.ModuleType
types.SimpleNamespace
types.TracebackType
types.new_class
types.prepare_class

但是我们并不推荐使用type来进行类型检查,之所以把这些类型列出来,也是为了扩展知识 面。那为什么不推荐使用type进行类型检查呢?我们来看一下下面的例子。

import types
class UserInt(int):
    def __init__(self, val=0):
        self.val = int(val)

i = 1
n = UserInt(2)
print(type(i) is type(n))

上面的代码输出:False

这就说明i和n的类型是不一样的,而实际上UserInt是继承自int的,所以这个判断是存在问题的, 当我们对Python内建类型进行扩展的时候,type返回的结果就不够准确了。我们再看一个例子。

class A():
    pass

class B():
    pass

a = A()
b = B()

print(type(a) is type(b))

代码的输出结果: True

type比较的结果a和b的类型是一样的,结果明显是不准确的。这种古典类的实例,type返回的结果都 是一样的,而这样的结果不是我们想要的。对于内建的基本类型来说,使用tpye来检查是没有问题的, 可是当应用到其他场合的时候,type就显得不可靠了。这个时候我们就需要使用isinstance来进行类型 检查。

isinstance(object, classinfo)

object表示实例,classinfo可以是直接或间接类名、基本类型或者有它们组成的元组。

>>> isinstance(2, float)
False
>>> isinstance('a', (str, unicode))
True
>>> isinstance((2, 3), (str, list, tuple))
True



原文地址:https://www.cnblogs.com/yanglang/p/7485208.html