- Python 3 教程
- Python3 基本数据类型
- Python3 解释器
- Python3 注释
- Python3 运算符
- Python3 数字(Number)
- Python3 字符串
- Python3 列表
- Python3 元组
- Python3 字典
- Python3 编程第一步
- Python3 条件控制
- Python3 循环语句
- Python3 迭代器与生成器
- Python3 函数
- Python3 数据结构
- Python3 模块
- Python3 输入和输出
- Python3 File(文件) 方法
- Python3 OS 文件/目录方法
- Python3 错误和异常
- Python3 面向对象
- Python3 标准库概览
- Python3 实例
- Python3 正则表达式
- Python CGI编程
- Python3 MySQL 数据库连接
- Python3 网络编程
- Python3 SMTP发送邮件
- Python3 多线程
- Python3 XML解析
- Python3 JSON 数据解析
- Python3 日期和时间
- Python3 range() 函数用法
- Python3 内置函数
Python isinstance() 函数
描述
isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
isinstance() 与 type() 区别:
type() 不会认为子类是一种父类类型,不考虑继承关系。
isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
语法
以下是 isinstance() 方法的语法:
isinstance(object, classinfo)
参数
- object -- 实例对象。
- classinfo -- 可以是直接或间接类名、基本类型或者有它们组成的元组。
返回值
如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False。。
实例
以下展示了使用 isinstance 函数的实例:
>>>a = 2
>>> isinstance (a,int)
True
>>> isinstance (a,str)
False
>>> isinstance (a,(str,int,list)) # 是元组中的一个返回 True
True
type() 与 isinstance()区别:
class A:
pass
class B(A):
pass
isinstance(A(), A) # returns True
type(A()) == A # returns True
isinstance(B(), A) # returns True
type(B()) == A # returns False
上一篇:Python any() 函数
下一篇:Python sum() 函数
关注微信小程序
扫描二维码
程序员编程王