python中isinstance用法_Python内置isinstance函数详细介绍
英文文档:
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 "", line 1, in
isinstance(a,[A,B,C])
TypeError: isinstance() arg 2 must be a type or tuple of types
python中isinstance用法_Python内置isinstance函数详细介绍相关推荐
- python中dir用法_Python内置函数dir详解
1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: >>> help(dir) Help on built-in function dir in mo ...
- python中bytearray函数_Python内置bytearray函数详细介绍
英文文档: classbytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray clas ...
- python中bool函数的作用_Python内置bool函数详细介绍
英文文档: classbool([x]) Return a Boolean value, i.e. one of True or False. x is converted using the sta ...
- int是python的内置函数吗_Python内置int函数详细介绍
英文文档: class int(x=0) class int(x, base=10) Return an integer object constructed from a number or str ...
- python布尔函数_Python内置bool函数详细介绍
英文文档: classbool([x]) Return a Boolean value, i.e. one of True or False. x is converted using the sta ...
- python模拟内置函数all_Python内置all函数详细介绍
英文文档: all(iterable) Return True if all elements of the iterable are true (or if the iterable is empt ...
- python中globals用法_Python基础教程之内置函数locals()和globals()用法分析
本文实例讲述了Python基础教程之内置函数locals()和globals()用法.分享给大家供大家参考,具体如下: 1. 这两个函数主要提供,基于字典的访问局部变量和全局变量的方式. python ...
- python自带笔记本电脑_Python内置常用模块
time和datatime Range os sys hashlib XML json & picle 1.time和datetime time和datetime都是python处理时间和日期 ...
- python中的作用域以及内置函数globals()-全局变量、locals()-局部变量
在python中,函数会创建一个自己的作用域,也称为为命名空间.这意味着在函数内部访问某个变量时,函数会优先在自己的命名空间中寻找. 通过内置函数globals()返回的是python解释器能知道的变 ...
最新文章
- css的content属性
- class mywnd : pubic qwidget是什么意思_学了这么久java反射,你知道class.forName和classloader的区别吗?...
- how to get context node reference CN0X from view controller reference
- html网页设如何置访问密码,利用JS给单页html加上简单访问密码,需要密码才能访问!...
- Dreamweaver 格式化代码
- 万恶之源 - Python基础知识补充
- 启动tomcat的startup.bat闪退问题
- 鸿蒙硬件HI3861点灯
- python面试题之解释一下python的and-or语法
- 【译】Tim Rose 的kibana插件教程-自定义App插件
- 微信内置浏览器中实现点击电话号码自动到拨号页面
- Jquery监听value的变化
- Python基础090:解决jupyter notebook无法自动跳转chrome浏览器的问题
- Power Analysis估算样本容量
- 标签打印软件制作标签时如何导入品牌logo
- pwnable.kr unlink
- PS怎样扣图片和压缩图片工具推荐
- 华为发展鸿蒙系统再出奇招,为了留存现有手机用户可谓费尽脑汁
- JAVA swing实现简单增删改查
- Unity3D——第一人称FPS生存游戏(resident zombies)
热门文章
- 视频回顾丨带你逛腾讯全球数字生态大会「腾讯技术工程」展区
- ubuntu 16.04 kvm 桥接模式创建虚拟机
- Android插件框架VirtualAPK
- go context之WithDeadline的使用
- elasticsearch说了一些了,这次说说Solr【入门Solr这篇就够了】
- Request method 'GET' not supported解决方式
- 数据结构:堆和败者树的区别是什么?
- 关于MySQL使用Float存储时的精度问题
- 【SpringBoot】浏览器报错Resource interpreted as Stylesheet but transferred with MIME type text/html
- ACM练习 链表排序 II 【WA】