如何在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正则表达式操作指南(re使用)
Sep 06 Python
Python中使用scapy模拟数据包实现arp攻击、dns放大攻击例子
Oct 23 Python
Python中使用Tkinter模块创建GUI程序实例
Jan 14 Python
Python3 获取一大段文本之间两个关键字之间的内容方法
Oct 11 Python
在Python中获取操作系统的进程信息
Aug 27 Python
python多线程分块读取文件
Aug 29 Python
Python调用shell cmd方法代码示例解析
Jun 18 Python
如何基于Python代码实现高精度免费OCR工具
Jun 18 Python
python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法
Nov 05 Python
Python模拟键盘输入自动登录TGP
Nov 27 Python
python实现登录与注册系统
Nov 30 Python
python如何读取.mtx文件
Apr 22 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
Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口
2007/04/18 PHP
PHP 自动加载的简单实现(推荐)
2016/08/12 PHP
PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)
2016/09/11 PHP
Laravel 框架基于自带的用户系统实现登录注册及错误处理功能分析
2020/04/14 PHP
javascript FormatNumber函数实现方法
2008/12/30 Javascript
JS 密码强度验证(兼容IE,火狐,谷歌)
2010/03/15 Javascript
IE6/7 and IE8/9/10(IE7模式)依次隐藏具有absolute或relative的父元素和子元素后再显示父元素
2011/07/31 Javascript
javaScript矢量图表库-gRaphael几行代码实现精美的条形图/饼图/点图/曲线图
2013/01/09 Javascript
jquery next nextAll nextUntil siblings的区别介绍
2013/10/05 Javascript
form.submit()不能提交表单的错误原因及解决方法
2014/10/13 Javascript
详解Javascript中的原型OOP
2016/10/12 Javascript
JS手机端touch事件计算滑动距离的方法示例
2017/10/26 Javascript
JavaScript数据结构与算法之二叉树实现查找最小值、最大值、给定值算法示例
2019/03/01 Javascript
微信小程序封装分享与分销功能过程解析
2019/08/13 Javascript
vue+element使用动态加载路由方式实现三级菜单页面显示的操作
2020/08/04 Javascript
小程序实现可拖动的悬浮按钮
2020/09/07 Javascript
Python装饰器基础详解
2016/03/09 Python
python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)
2017/03/12 Python
python之Character string(实例讲解)
2017/09/25 Python
Python3一行代码实现图片文字识别的示例
2018/01/15 Python
Django学习笔记之ORM基础教程
2018/03/27 Python
对Python中的条件判断、循环以及循环的终止方法详解
2019/02/08 Python
Python 利用邮件系统完成远程控制电脑的实现(关机、重启等)
2019/11/19 Python
利用python实现.dcm格式图像转为.jpg格式
2020/01/13 Python
html5 touch事件实现页面上下滑动效果【附代码】
2016/03/10 HTML / CSS
挪威户外活动服装和装备购物网站:Bergfreunde挪威
2016/10/20 全球购物
TCP/IP模型的分界线
2012/12/01 面试题
农村婚礼证婚词
2014/01/08 职场文书
大学生涯自我鉴定
2014/01/16 职场文书
捐书寄语赠言
2014/01/18 职场文书
聚美优品陈欧广告词
2014/03/14 职场文书
父母对孩子说的话
2014/04/12 职场文书
化工操作工岗位职责
2014/04/29 职场文书
小学毕业典礼演讲稿
2014/09/09 职场文书
领导班子个人查摆问题对照检查材料
2014/10/02 职场文书
Golang获取List列表元素的四种方式
2022/04/20 Golang