`
shopscor
  • 浏览: 19640 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

python中isinstance用法 (判断数据类型)

阅读更多
isinstance( object, classinfo)
    Return true if the object argument is an instance of the classinfo argument, or of a (direct or indirect) subclass thereof. Also return true if classinfo is a type object and object is an object of that type. If object is not a class instance or an object of the given type, the function always returns false. If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted). If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised. Changed in version 2.2: Support for a tuple of type information was added.


Python可以得到一个对象的类型 ,利用type函数:

>>>lst = [1, 2, 3]
>>>type(lst)
<type 'list'>


不仅如此,还可以利用isinstance函数,来判断一个对象是否是一个已知的类型。

isinstance说明如下:

    isinstance(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.).

其第一个参数为对象,第二个为类型名或类型名的一个列表。其返回值为布尔型。若对象的类型与参数二的类型相同则返回True。若参数二为一个元组,则若对象类型与元组中类型名之一相同即返回True。

>>>isinstance(lst, list)
Trueisinstance(lst, (int, str, list))
True

>>>isinstance(lst, (int, str, list))
True
分享到:
评论

相关推荐

    python数据类型判断type与isinstance的区别实例解析

    主要介绍了python数据类型判断type与isinstance的区别实例解析,具有一定参考价值,需要的朋友可以了解下。

    Python isinstance判断对象类型

    复制代码 代码如下:if (typeof(objA) == typeof(String)) { //TODO } 在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: 复制代码 代码如下:class objA: pass A = objA() B = ‘a’,’...

    Python检测数据类型的方法总结

    下面小编给大家分享一下在python中如何检测数据类型 首先我们打开CMD控制台,进入到python环境,然后声明一个列表,如下图所示 然后我们调用isinstance方法对列表变量进行类型判断,如下图所示,第二个参数是数据...

    Python中请使用isinstance()判断变量类型

    在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便。 复制代码 代码如下: # coding=utf-8   a = 10   def b():  pass   print isinstance(a,(int,str))...

    python isinstance函数用法详解

    这篇文章主要介绍了python isinstance函数用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 isinstance() 函数来判断一个对象是否是一个已知的类型类似 ...

    python区分不同数据类型的方法

    python怎么区分不同数据类型? Python判断变量的数据类型的两种方法 一、Python中的数据类型有数字、字符串,...python 判断数据类型 基本区别在于: type():不会认为子类是父类 isinstance():会认为子类是父类类型 cl

    对python中assert、isinstance的用法详解

    使用范例: assert 3 == 3 assert 1 == True assert (4 == 4) print('-----------') assert (3 == 4) ''' 抛出AssertionError异常,后面程序不执行 ''' print('-----------') 输出结果: D:\Users\lenov

    python issubclass 和 isinstance函数

    主要介绍了python issubclass 和 isinstance函数,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下

    Python知识点(史上最全).pdf

    复数 complex 由实数和虚数组成 Python中有6个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionart(字典) 其中不可变得数据: Number(数字) String(字符串) Tuple(元组) ...

    Python isinstance函数介绍

    判断变量是否是这个类型  复制代码 代码如下:  class objA:  pass    A = objA()  B = ‘a’,’v’  C = ‘a string’    print isinstance(A, objA)  print isinstance(B, tuple)  print isinstance(C, ...

    如何在python中判断变量的类型

    python的数据类型有:数字(int)、浮点(float)、字符串(str),列表(list)、元组(tuple)、字典(dict)、集合(set) 一般通过以下方法进行判断: 1、isinstance(参数1,参数2) 描述:该函数用来判断一个变量(参数1)是否...

    Python菜鸟教程.docx

    标准数据类型 Python3 中有六个标准的数据类型: Number(数字)、String(字符串)、List(列表)、Tuple(元组)、Set(集合)、Dictionary(字典) Python3 的六个标准数据类型中: 不可变数据(3 个):Number...

    python求素数因子-Python入门教程:素数判断与素因子分解.pdf

    isprime函数 # -*- coding: utf-8 -*- def isprime(num: int) -&gt; bool: if not isinstance(num, int): raise TypeError if num * p 这⾥⽤到了定义函数时, 进⾏输⼊参数的类型判断。 想⼀想, 如何添加输出参数的

    浅谈Python中重载isinstance继承关系的问题

    本篇文章主要介绍了浅谈Python中重载isinstance继承关系的问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    python-adt:Python 代数数据类型,使用 Hy!

    Python 代数数据类型该项目旨在使用语言在 Python 中实现代数数据类型。 实际上,我们想输入这样的代码(data TypeName (TypeConstructor (name str)) :deriving (Show, Eq )) 并使用宏获得类似以下内容。 class ...

Global site tag (gtag.js) - Google Analytics