python之函数用法isinstance()

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之函数用法isinstance()



#isinstance()
#说明:返回一个布尔值,判断数据类型
'''
isinstance(...)
    isinstance(object, class-or-type-or-tuple) -> bool
    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.).
'''


#案例
object=range(5)#list
print isinstance(object,tuple)#False
print isinstance(object,list)#True


object='xiaodeng'
print isinstance(object,(str,int))#True;同时传递多个数据类型
print isinstance(object,(str,int,list))#True;同时传递多个数据类型

 

posted @ 2015-11-07 17:06  Xiao|Deng  阅读(869)  评论(0编辑  收藏  举报