如何在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比较两个图片相似度的方法
Mar 13 Python
python对指定目录下文件进行批量重命名的方法
Apr 18 Python
Python使用chardet判断字符编码
May 09 Python
Python常用的爬虫技巧总结
Mar 28 Python
python版本的读写锁操作方法
Apr 25 Python
python实现二维数组的对角线遍历
Mar 02 Python
python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法
Jul 04 Python
python GUI库图形界面开发之PyQt5打印控件QPrinter详细使用方法与实例
Feb 28 Python
python实现扫雷游戏
Mar 03 Python
python实现读取类别频数数据画水平条形图案例
Apr 24 Python
python matplotlib模块基本图形绘制方法小结【直线,曲线,直方图,饼图等】
Apr 26 Python
python如何利用Mitmproxy抓包
Oct 10 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中文汉字验证码
2007/04/08 PHP
PHP 得到根目录的 __FILE__ 常量
2008/07/23 PHP
PHP5 操作MySQL数据库基础代码
2009/09/29 PHP
php获取服务器信息的实现代码
2013/02/04 PHP
php判断表是否存在的方法
2015/06/18 PHP
Laravel 验证码认证学习记录小结
2019/12/20 PHP
php如何获取Http请求
2020/04/30 PHP
JavaScript游戏之是男人就下100层代码打包
2010/11/08 Javascript
基于jquery的文章中所有图片width大小批量设置方法
2013/08/01 Javascript
原生js ActiveXObject获取execl里面的值
2013/11/01 Javascript
javascript原始值和对象引用实例分析
2015/04/25 Javascript
JS组件Bootstrap Table布局详解
2016/05/27 Javascript
jQuery数据检索中根据关键字快速定位GridView指定行的实现方法
2016/06/08 Javascript
JS传播事件、取消事件默认行为、阻止事件传播详解
2017/08/14 Javascript
node通过express搭建自己的服务器
2017/09/30 Javascript
vue2.0 axios跨域并渲染的问题解决方法
2018/03/08 Javascript
angular4 共享服务在多个组件中数据通信的示例
2018/03/30 Javascript
js前端面试之同步与异步问题详解
2019/04/03 Javascript
vue+layui实现select动态加载后台数据的例子
2019/09/20 Javascript
vue实现点击按钮下载文件功能
2019/10/11 Javascript
vue中的使用token的方法示例
2020/03/10 Javascript
JavaScript实现简单日历效果
2020/09/11 Javascript
[02:43]DOTA2英雄基础教程 德鲁伊
2014/01/13 DOTA
[52:03]Secret vs VG 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
[01:20:37]FNATIC vs NIP 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
python打开网页和暂停实例
2014/09/30 Python
Python使用QRCode模块生成二维码实例详解
2017/06/14 Python
Python内置模块turtle绘图详解
2017/12/09 Python
大学生旅游业创业计划书
2014/01/29 职场文书
数学兴趣小组活动总结
2014/07/08 职场文书
社区母亲节活动总结
2015/02/10 职场文书
汤姆索亚历险记读书笔记
2015/06/29 职场文书
python tkinter模块的简单使用
2021/04/07 Python
Go语言 go程释放操作(退出/销毁)
2021/04/30 Golang
使用pycharm运行flask应用程序的详细教程
2021/06/07 Python
vue实现列表垂直无缝滚动
2022/04/08 Vue.js