如何在python中判断变量的类型


Posted in Python onJuly 29, 2020

python的数据类型有:数字(int)、浮点(float)、字符串(str),列表(list)、元组(tuple)、字典(dict)、集合(set)

一般通过以下方法进行判断:

1、isinstance(参数1,参数2)

描述:该函数用来判断一个变量(参数1)是否是已知的变量类型(参数2) 类似于type()

参数1:变量

参数2:可以是直接或间接类名、基本类型或者由它们组成的元组。

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

例子:

#判断变量类型的函数
def typeof(variate):
  type=None
  if isinstance(variate,int):
    type = "int"
  elif isinstance(variate,str):
    type = "str"
  elif isinstance(variate,float):
    type = "float"
  elif isinstance(variate,list):
    type = "list"
  elif isinstance(variate,tuple):
    type = "tuple"
  elif isinstance(variate,dict):
    type = "dict"
  elif isinstance(variate,set):
    type = "set"
  return type
# 返回变量类型
def getType(variate):
  arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
  vartype = typeof(variate)
  if not (vartype in arr):
    return "未知类型"
  return arr[vartype]

#判断变量是否为整数
money=120
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为字符串
money="120"
print("{0}是{1}".format(money,getType(money)))
money=12.3
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为列表
students=['studentA']
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为元组
students=('studentA','studentB')
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为字典
dictory={"key1":"value1","key2":"value2"}
print("{0}是{1}".format(dictory,getType(dictory)))
#判断变量是否为集合
apple={"apple1","apple2"}46 print("{0}是{1}".format(apple,getType(apple)))

返回:

如何在python中判断变量的类型

2、通过与已知类型的常量进行比较

例子:

#判断变量类型的函数
def typeof(variate):
  type1 = ""
  if type(variate) == type(1):
    type1 = "int"
  elif type(variate) == type("str"):
    type1 = "str"
  elif type(variate) == type(12.3):
    type1 = "float"
  elif type(variate) == type([1]):
    type1 = "list"
  elif type(variate) == type(()):
    type1 = "tuple"
  elif type(variate) == type({"key1":"123"}):
    type1 = "dict"
  elif type(variate) == type({"key1"}):
    type1 = "set"
  return type1
# 返回变量类型
def getType(variate):
  arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
  vartype = typeof(variate)
  if not (vartype in arr):
    return "未知类型"
  return arr[vartype]

#判断变量是否为整数
money=120
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为字符串
money="120"
print("{0}是{1}".format(money,getType(money)))
money=12.3
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为列表
students=['studentA']
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为元组
students=('studentA','studentB')
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为字典
dictory={"key1":"value1","key2":"value2"}
print("{0}是{1}".format(dictory,getType(dictory)))
#判断变量是否为集合
apple={"apple1","apple2"}
print("{0}是{1}".format(apple,getType(apple)))

返回:

如何在python中判断变量的类型

补充: 

isinstance() 与 type() 区别:

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

如果要判断两个类型是否相同推荐使用 isinstance()。

以上就是如何在python中判断变量的类型的详细内容,更多关于Python判断变量类型的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
利用python程序帮大家清理windows垃圾
Jan 15 Python
Python语言描述连续子数组的最大和
Jan 04 Python
ubuntu17.4下为python和python3装上pip的方法
Jun 12 Python
20行python代码实现人脸识别
May 05 Python
利用Python进行图像的加法,图像混合(附代码)
Jul 14 Python
对Python函数设计规范详解
Jul 19 Python
python3.6 tkinter实现屏保小程序
Jul 30 Python
Golang GBK转UTF-8的例子
Aug 26 Python
Python简易计算器制作方法代码详解
Oct 31 Python
python 生成任意形状的凸包图代码
Apr 16 Python
python爬虫今日热榜数据到txt文件的源码
Feb 23 Python
Python3自带工具2to3.py 转换 Python2.x 代码到Python3的操作
Mar 03 Python
Python中的With语句的使用及原理
Jul 29 #Python
解决c++调用python中文乱码问题
Jul 29 #Python
Python 实现简单的客户端认证
Jul 29 #Python
Tensorflow使用Anaconda、pycharm安装记录
Jul 29 #Python
学python爬虫能做什么
Jul 29 #Python
Python 创建TCP服务器的方法
Jul 28 #Python
Python实现画图软件功能方法详解
Jul 28 #Python
You might like
PHP VS ASP
2006/10/09 PHP
php抓取https的内容的代码
2010/04/06 PHP
php中关于codeigniter的xmlrpc的类在进行数据交换时的类型问题
2011/07/03 PHP
PHP函数超时处理方法
2016/02/14 PHP
PHP面向对象中new self()与 new static()的区别浅析
2017/08/17 PHP
Js控制弹窗实现在任意分辨率下居中显示
2013/08/01 Javascript
JavaScript简单表格编辑功能实现方法
2015/04/16 Javascript
利用jQuery插件imgAreaSelect实现获得选择域的图像信息
2016/12/02 Javascript
Bootstrap CSS组件之面包屑导航(breadcrumb)
2016/12/17 Javascript
windows 下安装nodejs 环境变量设置
2017/02/02 NodeJs
基于Vue2实现的仿手机QQ单页面应用功能(接入聊天机器人 )
2017/03/30 Javascript
详解微信小程序 登录获取unionid
2017/06/27 Javascript
JScript实现表格的简单操作
2017/08/15 Javascript
把vue-router和express项目部署到服务器的方法
2018/02/21 Javascript
Angular4 ElementRef的应用
2018/02/26 Javascript
JavaScript去掉数组重复项的方法分析【测试可用】
2018/07/19 Javascript
详解在create-react-app使用less与antd按需加载
2018/12/06 Javascript
bootstrapValidator表单校验、更改状态、新增、移除校验字段的实例代码
2020/05/19 Javascript
js仿淘宝放大镜效果
2020/12/28 Javascript
玩转python爬虫之正则表达式
2016/02/17 Python
利用python将json数据转换为csv格式的方法
2018/03/22 Python
Python中pillow知识点学习
2018/04/30 Python
numpy添加新的维度:newaxis的方法
2018/08/02 Python
python自动发邮件总结及实例说明【推荐】
2019/05/31 Python
django 捕获异常和日志系统过程详解
2019/07/18 Python
澳大利亚连衣裙和女装在线:Esther
2017/11/11 全球购物
定义一结构体变量,用其表示点坐标,并输入两点坐标,求两点之间的距离
2015/08/17 面试题
开业庆典策划方案
2014/02/18 职场文书
会计专业求职信范文
2014/03/16 职场文书
新人入职感言
2015/07/31 职场文书
健康教育主题班会
2015/08/14 职场文书
2019各种承诺书范文
2019/06/24 职场文书
致创业的您:这类人不适合餐饮创业
2019/08/19 职场文书
总结Python常用的魔法方法
2021/05/25 Python
24年收藏2000多部退役军用电台
2022/02/18 无线电
Android基础入门之dataBinding的简单使用教程
2022/06/21 Java/Android