如何在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 远程统计文件代码分享
May 14 Python
Python编程中对super函数的正确理解和用法解析
Jul 02 Python
Python中datetime模块参考手册
Jan 13 Python
python八大排序算法速度实例对比
Dec 06 Python
pandas获取groupby分组里最大值所在的行方法
Apr 20 Python
python 定义n个变量方法 (变量声明自动化)
Nov 10 Python
Python中作用域的深入讲解
Dec 10 Python
python地震数据可视化详解
Jun 18 Python
Python 使用 PyMysql、DBUtils 创建连接池提升性能
Aug 14 Python
使用Python打造一款间谍程序的流程分析
Feb 21 Python
python和c语言哪个更适合初学者
Jun 22 Python
如何用 Python 子进程关闭 Excel 自动化中的弹窗
May 07 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操作MongoDB时的整数问题及对策说明
2011/05/02 PHP
使用PHP实现阻止用户上传成人照片或者裸照
2014/12/25 PHP
Yii学习总结之安装配置
2015/02/22 PHP
不用构造函数(Constructor)new关键字也能实现JavaScript的面向对象
2013/01/11 Javascript
用jQuery toggleClass 实现鼠标移上变色
2014/05/14 Javascript
利用javascript实现全部删或清空所选的操作
2014/05/27 Javascript
Jquery $.getJSON 在IE下的缓存问题解决方法
2014/10/10 Javascript
JS实现跟随鼠标立体翻转图片的方法
2015/05/04 Javascript
javascript通过获取html标签属性class实现多选项卡的方法
2015/07/27 Javascript
推荐10 个很棒的 jQuery 特效代码
2015/10/04 Javascript
浅谈javascript中new操作符的原理
2016/06/07 Javascript
JavaScript跨域调用基于JSON的RESTful API
2016/07/09 Javascript
使用bootstrap-paginator.js 分页来进行ajax 异步分页请求示例
2017/03/09 Javascript
使用JavaScript根据图片获取条形码的方法
2017/07/04 Javascript
详解VUE 对element-ui中的ElTableColumn扩展
2018/03/28 Javascript
js变量声明var使用与不使用的区别详解
2019/01/21 Javascript
jQuery实现经典的网页3D轮播图封装功能【附源码下载】
2019/02/15 jQuery
electron实现静默打印的示例代码
2019/08/12 Javascript
Vue中使用matomo进行访问流量统计的实现
2019/11/05 Javascript
[03:22]DAC最前线(第二期)—DOTA2亚洲邀请赛主赛场周边及线路探访
2015/01/24 DOTA
python使用append合并两个数组的方法
2015/04/28 Python
在Python下尝试多线程编程
2015/04/28 Python
对Python Class之间函数的调用关系详解
2019/01/23 Python
详解Tensorflow不同版本要求与CUDA及CUDNN版本对应关系
2020/08/04 Python
python asyncio 协程库的使用
2021/01/21 Python
纯css3(无图片/js)制作的几个社交媒体网站的图标
2013/03/21 HTML / CSS
从一次项目重构说起CSS3自定义变量在项目的使用方法
2021/03/01 HTML / CSS
Mixbook加拿大:照片书,照片卡,剪贴簿,年历和日历
2017/02/21 全球购物
新加坡一家在线男士皮具品牌:Faire Leather Co.
2019/12/01 全球购物
一封普通求职者的求职信
2013/11/20 职场文书
2014年会策划方案
2014/05/11 职场文书
办公室主任个人对照检查材料思想汇报
2014/10/11 职场文书
开展批评与自我批评心得体会
2014/10/17 职场文书
2015初中团支部工作总结
2015/07/21 职场文书
男方家长婚礼答谢词
2015/09/29 职场文书
回门宴新娘答谢词
2015/09/29 职场文书