英文文档:

isinstance(object, classinfo)

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.

说明:

  1. 函数功能用于判断对象是否是类型对象的实例,object参数表示需要检查的对象,calssinfo参数表示类型对象。

  2. 如果object参数是classinfo类型对象(或者classinfo类对象的直接、间接、虚拟子类)的实例,返回True。

>>> isinstance(1,int)
True
>>> isinstance(1,str)
False # 定义3各类:C继承B,B继承A
>>> class A:
pass >>> class B(A):
pass >>> class C(B):
pass >>> a = A()
>>> b = B()
>>> c = C()
>>> isinstance(a,A) #直接实例
True
>>> isinstance(a,B)
False
>>> isinstance(b,A) #子类实例
True
>>> isinstance(c,A) #孙子类实例
True

  3. 如果object参数传入的是类型对象,则始终返回False。

>>> isinstance(str,str)
False
>>> isinstance(bool,int)
False

  4. 如果classinfo类型对象,是多个类型对象组成的元组,如果object对象是元组的任一类型对象中实例,则返回True,否则返回False。

>>> isinstance(a,(B,C))
False
>>> isinstance(a,(A,B,C))
True

  5. 如果classinfo类型对象,不是一个类型对象或者由多个类型对象组成的元组,则会报错(TypeError)。

>>> isinstance(a,[A,B,C])
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
isinstance(a,[A,B,C])
TypeError: isinstance() arg 2 must be a type or tuple of types

Python内置函数(34)——isinstance的更多相关文章

  1. Python内置函数(49)——isinstance

    英文文档: isinstance(object, classinfo) Return true if the object argument is an instance of the classin ...

  2. Python内置函数之isinstance,issubclass

    isinstance判断一个变量的类型 >>> n1 = 10>>> isinstance (n1,int)True 判断n1是否是数字类型,如果是返回True如果 ...

  3. Python内置函数之isinstance()

    isinstance(object,classinfo)用来判断对象是否为某种数据类型. 例子: >>> isinstance(,object) True >>> ...

  4. Python内置函数(34)——map

    英文文档: map(function, iterable, ...) Return an iterator that applies function to every item of iterabl ...

  5. Python内置函数(34)——filter

    英文文档: filter(function, iterable) Construct an iterator from those elements of iterable for which fun ...

  6. python 内置函数的补充 isinstance,issubclass, hasattr ,getattr, setattr, delattr,str,del 用法,以及元类

    isinstance   是 python中的内置函数 , isinstance()用来判断一个函数是不是一个类型 issubclass  是python 中的内置函数,  用来一个类A是不是另外一个 ...

  7. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  8. python内置函数简单归纳

    做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...

  9. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

随机推荐

  1. 559. Maximum Depth of N-ary Tree

    https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ 非常简单的题目,连edge case 都没有.思路就是:最 ...

  2. Java类的继承与方法调用的一个小问题

    public class Father { protected void server(int i){ switch (i){ case 1: methodone(); break; case 2: ...

  3. 关于Hbase的预分区,解决热点问题

    Hbase默认建表是只有一个分区的,开始的时候所有的数据都会查询这个分区,当这个分区达到一定大小的时候,就会进行做split操作: 因此为了确保regionserver的稳定和高效,应该尽量避免reg ...

  4. 业务线接入前端异常监控sentry

    1.前端异常处理的框架对比   是否开源 收费 语言 监控范围 sentry 是 自己搭建服务器(免费)价格 英文 Angular.AngularJs.Backbone.Ember.JavaScrip ...

  5. POJ 3751 JAVA

    题意: 对于给定的采用”yyyy/mm/dd”加24小时制(用短横线”-”连接)来表示日期和时间的字符串, 请编程实现将其转换成”mm/dd/yyyy”加12小时制格式的字符串,末尾加上pm或者am. ...

  6. [Python]Python中的包(Package)

    参考官方文档中的Module和Glosssary中的描述.Module: https://docs.python.org/3/tutorial/modules.html#packagesGlossar ...

  7. C++的编译预处理

    C++中,在编译器对源程序进行编译之前,首先要由预处理对程序文本进行预处理.预处理器提供了一组预编译处理指令和预处理操作符.预处理指令实际上不是C++语言的一部分,它只是用来扩充C++程序设计的环境. ...

  8. mvc 路由配置

    1.URL模式 路由系统用一组路由来实现它的功能,这些路由共同组成了应用系统URL架构或方案,这种URL架构是应用程序能够识别并能对之做出响应的一组URL,当处理一个输入 请求时,路由系统的工作是将这 ...

  9. spring创建bean的三种方式

    spring创建bean的三种方式: 1通过构造方法创建bean(最常用) 1.1 spring默认会通过无参构造方法来创建bean,如果xml文件是这样配置,则实体类中必须要有无参构造方法,无参构造 ...

  10. SpringBoot 中 @RestController 和 @Controller 的区别

    1 - 在springboot中,@RestController 相当于 @Controller + @ResponseBody;2 - 即在Controller类中,若想返回jsp或html页面,则 ...