云南网站建设方法,宣城做网站,黑龙江网络推广好做吗,学生个人主页模板目录
一.Python type 函数简介二.Python isinstance 函数简介三.Python type 函数和 isinstance 函数区别四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 Python 基础入门 Python 变量#xff0c;也称 Python 数据类型。Python 变量一共六种类型#x…目录
一.Python type 函数简介二.Python isinstance 函数简介三.Python type 函数和 isinstance 函数区别四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 Python 基础入门 Python 变量也称 Python 数据类型。Python 变量一共六种类型整数/浮点数/字符串/BOOL/列表/元组/字典
一.Python type 函数简介
**Python 内置函数 type该函数主要用于解析判断 Python 变量类型**type 函数语法如下 函数描述type 函数用于获取变量类型
参数object : 实例对象
返回值直接或者间接类名、基本类型type(object)二.Python isinstance 函数简介
isinstance 函数是 **Python **中的一个内置函数主要用于检测变量类型返回值是 bool 值 isinstance 函数语法如下 函数描述主要用于检测变量类型返回值是 bool 值
参数object : 实例对象。classinfo : 可以是直接或者间接类名、基本类型或者由它们组成的元组。
返回值如果对象的类型与classinfo类型相同则返回 True否则返回 False。
isinstance(object,classinfo)三.Python type 函数和 isinstance 函数区别
** isinstance 函数会认为子类是一种父类类型考虑继承关系。**** type 函数不会认为子类是一种父类类型不考虑继承关系。**
# !usr/bin/env python# -_- coding:utf-8 \__-Author:猿说编程Blog(个人博客地址): www.codersrc.comFile:python type 函数和 isinstance 函数区别.pyTime:2021/3/21 11:37Motto:不积跬步无以至千里不积小流无以成江海程序人生的精彩需要坚持不懈地积累class Animation:passclass Dog(Animation):passprint(isinstance(Animation(), Animation)) # returns Trueprint(type(Animation()) Animation) # returns Trueprint(isinstance(Dog(), Animation)) # returns Trueprint(type(Dog()) Animation) # returns False输出结果:TrueTrueTrueFalse代码分析
创建一个 Animation 对象再创建一个继承 Animation 对象的 Dog 对象使用 isinstance 和 type 来比较 Animation 和 Animation 时由于它们的类型都是一样的所以都返回了 True。
而 Dog 对象继承于 Animation 对象在使用 isinstance 函数来比较 Dog 和 Animation 时由于考虑了继承关系所以返回了 True使用 type 函数来比较 Dog 和 Animation 时不会考虑 Dog 继承自哪里所以返回了 False。
** 总结如果要判断两个类型是否相同则推荐使用 isinstance 函数**;
四.猜你喜欢
Python 简介Python Pycharm Anacanda 区别Python2.x 和 Python3.x如何选择Python 配置环境Python Hello World 入门Python 代码注释Python 中文编码Anaconda 是什么Anconda 下载安装教程Pycharm 提示this license **** has been cancelledPycharm 设置开发模板/字体大小/背景颜色
未经允许不得转载猿说编程 » Python type 函数和 isinstance 函数区别