如何在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实现的ini文件操作类分享
Nov 20 Python
Python中使用select模块实现非阻塞的IO
Feb 03 Python
使用Python操作Elasticsearch数据索引的教程
Apr 08 Python
Python的组合模式与责任链模式编程示例
Feb 02 Python
获取Django项目的全部url方法详解
Oct 26 Python
详解Python在七牛云平台的应用(一)
Dec 05 Python
基于Python 装饰器装饰类中的方法实例
Apr 21 Python
Python爬取数据并写入MySQL数据库的实例
Jun 21 Python
如何利用Anaconda配置简单的Python环境
Jun 24 Python
Python在cmd上打印彩色文字实现过程详解
Aug 07 Python
Pytorch模型转onnx模型实例
Jan 15 Python
详解python itertools功能
Feb 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设计模式 Decorator(装饰模式)
2011/06/26 PHP
php去除换行符的方法小结(PHP_EOL变量的使用)
2013/02/16 PHP
php设计模式之职责链模式实例分析【星际争霸游戏案例】
2020/03/27 PHP
JavaScript中出现乱码的处理心得
2009/12/24 Javascript
jQuery 学习6 操纵元素显示效果的函数
2010/02/07 Javascript
jquery中each遍历对象和数组示例
2014/08/05 Javascript
jQuery根据ID获取input、checkbox、radio、select的示例
2014/08/11 Javascript
JQuery仿小米手机抢购页面倒计时效果
2014/12/16 Javascript
jQuery中:contains选择器用法实例
2014/12/30 Javascript
60行js代码实现俄罗斯方块
2015/03/31 Javascript
关于js里的this关键字的理解
2015/08/17 Javascript
Javascript点击其他任意地方隐藏关闭DIV实例
2016/06/21 Javascript
瀑布流的实现方式(原生js+jquery+css3)
2020/06/28 Javascript
使用veloticy-ui生成文字动画效果
2018/02/08 Javascript
layui监听工具栏的实例(操作列表按钮)
2019/09/10 Javascript
[05:22]DOTA2 2015国际邀请赛中国区预选赛首日TOP10
2015/05/26 DOTA
[02:10]探秘浦东源深体育馆 DOTA2 Supermajor不见不散
2018/05/17 DOTA
[01:06]DOTA2小知识课堂 Ep.01 TP出门不要忘记帮队友灌瓶哦
2019/12/05 DOTA
python计算最大优先级队列实例
2013/12/18 Python
python实现在cmd窗口显示彩色文字
2019/06/24 Python
python实现LBP方法提取图像纹理特征实现分类的步骤
2019/07/11 Python
numpy库reshape用法详解
2020/04/19 Python
python爬虫泛滥的解决方法详解
2020/11/25 Python
CSS3制作酷炫的条纹背景
2017/11/09 HTML / CSS
关于html字符串正则判断和匹配的具体使用
2019/12/12 HTML / CSS
美国创意礼品网站:UncommonGoods
2017/02/03 全球购物
微软加拿大官方网站:Microsoft Canada
2019/04/28 全球购物
毕业生自荐书模版
2014/01/04 职场文书
小学生演讲稿大全
2014/04/25 职场文书
综治宣传月活动总结
2014/04/28 职场文书
化工专业自荐书
2014/06/16 职场文书
2015年党员创先争优承诺书
2015/01/22 职场文书
礼仪培训心得体会
2016/01/22 职场文书
javascript数组includes、reduce的基本使用
2021/07/02 Javascript
Java练习之潜艇小游戏的实现
2022/03/16 Java/Android
教你win10系统中APPCRASH事件问题解决方法
2022/07/15 数码科技