基本类型

  • classinfo可以是,int,float,bool,complex,str(字符串),list,dict(字典),set,tuple

1、python对象的类型判断 -- isinstance()

  • isinstance() 函数来判断一个对象是否是一个已知的类型;
  • isinstance() 会认为子类是一种父类类型,考虑继承关系;
  • 如果要判断两个类型是否相同推荐使用 isinstance()。

语法:

isinstance(object, classinfo)

参数

  • object -- 实例对象。
  • classinfo -- 可以是直接或间接类名、基本类型或者由它们组成的元组。

返回值

如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False

1)基本类型  isinstance()

age = 20
name = "小明"
flag = True
price = 20.01print(isinstance(age, int))  # True
print(isinstance(name, str))  # True
print(isinstance(flag, bool))  # True
print(isinstance(price, float))  # Trueprint(isinstance(price, (int, list, str)))   # False, 是元组中的一个返回 True
print(isinstance(age, (int, list, str)))   # True,是元组中的一个返回 True

2)自定义类型  isinstance()

  • Father类的对象father,是Father类型;
  • Son类的对象son,是父类Father类型;
  • Son类的对象son,是Son类型;
  • Father类的对象father,不是Son类型;
class Father(object):passclass Son(Father):passfather = Father()
son = Son()
print(isinstance(father, Father))  # True
print(isinstance(son, Father))  # True
print(isinstance(son, Son))  # True
print(isinstance(father, Son))  # False

2、python对象的类型判断 -- type()

  • type() 不会认为子类是一种父类类型,不考虑继承关系。

1)基本类型  type()

age = 20
name = "小明"
flag = True
price = 20.01
print(type(age) == int)  # True
print(type(name) == str)  # True
print(type(flag) == bool)  # True
print(type(price) == float)  # True
print(type(price) in (int, list, str))   # False, 是元组中的一个返回 True
print(type(age) in (int, list, str))   # True,是元组中的一个返回 True

2)自定义类型  type()

  • Father类的对象father,是Father类型;
  • Son类的对象son,是Son类型;
  • Son类的对象son,不是父类Father类型;
  • Father类的对象father,不是Son类型;
class Father(object):passclass Son(Father):passfather = Father()
son = Son()
print(type(father) == Father)  # True
print(type(son) == Son)  # True
print(type(son) == Father)  # False
print(type(father) == Son)  # False

python对象的类型判断isinstance()、 type()相关推荐

  1. 聊一聊Python的变量类型判断type和isinstance

    正常情况下不应该编写代码检查类型的,而应该直接假设被操作的instance具有你希望的属性,否则抛出异常. class Parrot:def fly(self):print("Parrot ...

  2. python判断对象类型_如何查看一个Python对象的类型

    怎么查看一个对象的类型 在Python中有两种类型判断函数,type()和isinstance(). 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type()判 ...

  3. 全面理解Python中的类型提示(Type Hints)

    众所周知,Python 是动态类型语言,运行时不需要指定变量类型.这一点是不会改变的,但是2015年9月创始人 Guido van Rossum 在 Python 3.5 引入了一个类型系统,允许开发 ...

  4. Python中的类型判断方法介绍

    一.介绍 Python中判断变量类型可以使用以下2种方法: isinstance() type() 区别: type() 不会认为子类是一种父类类型,不考虑继承关系: isinstance() 会认为 ...

  5. 在python中类型属于对象变量是没有类型的_如何理解python对象有类型,变量无类型...

    在Python中,有这样一句话是非常重要的:对象有类型,变量无类型.怎么理解呢? 首先,5.6都是整数,Python中为它们取了一个名字,叫做"整数"类型的对象(或者数据),也可以 ...

  6. python 函数参数类型判断(判断类型)

    方法一 return (type(obj).__name__ == 'dict') 字典:dict  列表:list  集合:set 方法二 if isinstance(path_tmpls, str ...

  7. jQuery使用(十二):工具方法之type()之类型判断

    type()的使用 类型判断方法之is...() 实现原理可以参考我的另一篇js源码剖析博客: 类型和原生函数及类型转换(二:终结js类型判断) $.type( undefined ) === &qu ...

  8. python内建函数测试对象身份_Python学习笔记 03 Python对象

    1.Python对象 Python对象都拥有三个特性:身份.类型和值. 身份:每一个对象都有一个唯一的身份标识自己,任何对象的身份都可以使用内建函数id()来得到.这个值可以被认为是该对象的内存地址. ...

  9. 【Python】什么是python对象

    python使用对象模型来存储数据,也就是说构造任何类型的值都是一个对象.所有的python对象都拥有三个特性:身份id,类型和值. 身份id:唯一的身份标识,可以使用内建函数id()来得到,这个值可 ...

  10. python 学习笔记day03-python基础、python对象、数字、函数

    python基础 语句和语法 注释及续行 首要说明的是:尽管python可读性最好的语言之一,这并不意味者程序员在代码中就可以不写注释 和很多UNIX脚本类似,python注释语句从#字符开始 注释可 ...

最新文章

  1. 洛谷P3572 [POI2014]PTA-Little Bird
  2. 数据库中间件 MyCAT源码分析:【单库单表】插入【推荐阅读】
  3. Java 异常处理的误区和经验总结
  4. 极端情况下收缩 Go 进程的线程数
  5. linux 单步启动_Linux系统的启动
  6. java 从未导入_Java 8的10个您从未听说过的功能
  7. iOS开发学无止境 - NSFileManager文件操作的十个小功能
  8. 性能测试学习05_lr(根据接口文档写脚本+参数化)
  9. 3成失眠者放下手机才能睡 说中你了吗?
  10. ftps(ftp+ssl)
  11. c++ string substr_用std::string_view替换leveldb的Slice
  12. 涨姿势系列之——内核环境下花式获得CSRSS进程id
  13. html简单随机抽奖页面(在线抽奖、随机选取、自动挑选)
  14. java初级程序员成长之路
  15. Navicat删除注册表
  16. 如何正确、高效地进行论文阅读和批判性思考
  17. 怎样做出完美的高达模型
  18. css hover变成手_css鼠标样式cursor介绍(鼠标手型)
  19. 邯郸一中高考2021成绩查询,邯郸一中2019高考成绩喜报、本一本二上线人数情况...
  20. hacking 麦步手表之(2)命令行编译工程

热门文章

  1. 玩转3D全息图像!AI即刻生成
  2. excel服务器运行失败怎么办,解决勤哲EXCEL服务器启动失败的问题
  3. 一文告诉你市面上最火的游戏都是用什么引擎做的!!!
  4. Java显示smart3d数据_[转][smart3d]Smart3D之手动配置 S3C 索引加载全部的OSGB瓦片数据...
  5. JavaScript 高级程序设计 笔记
  6. 如何快速合并多个PDF文件或图片为一个PDF文件?
  7. w3c+android,w3cschool官方版app
  8. 推荐几本数据库基础书
  9. IDC机房建设的关键技术有哪些?
  10. 无人机项目跟踪记录七十七----蓝牙模块详解