如何在python中判断变量的类型
作者:小铭博客 发布时间:2022-05-03 03:35:06
标签:Python,判断,变量,类型
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)))
返回:
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)))
返回:
补充:
isinstance() 与 type() 区别:
type() 不会认为子类是一种父类类型,不考虑继承关系。
isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
来源:https://www.cnblogs.com/xmnote/p/9334743.html
0
投稿
猜你喜欢
- 原文:http://blog.rexsong.com/?p=746#comments加速的关键,不是降低重量,而是减少个数。如果重量在200
- 谁在用这些导航google是个大公司,全世界都有google的脚印,韩国的google动画效果非常不错,蓝色理想论坛里已经有人挖过来了,可惜
- 场景一:A网站全站均为UTF-8编码,B网站全站为GB2312编码。A网站提供一段JS代码供B网站调用,该代码会动态生成一个FORM表单,以
- 我准备在ASP中连接MYSQL了,请问如何做?首先要正确安装MYSQLX,装好之后,可调用以下程序即可正常访问MYSQL:<%@&nb
- 本文实例为大家分享了python爬取微信公众号文章的具体代码,供大家参考,具体内容如下# -*- coding: utf-8 -*-impo
- 本文实例讲述了Python中super函数用法。分享给大家供大家参考,具体如下:这是个高大上的函数,在python装13手册里面介绍过多使用
- 例如我们有如下结构的文件:pkg/ __init__.py libs/ some_lib.py __init__.py components
- 平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的《最佳日志实践》觉得写一个清晰明了,结构分明的日志还是非常有必要的。在写日
- 一.环境搭建1.下载安装包访问 Python官网下载地址:https://www.python.org/downloads/下载适合自己系统
- 1、在全局settings文件中配置```MEDIA_URL = '/media/'MEDIA_ROOT = os.path
- 1、简介在python自动化中,我们传递一些参数是需要从文件中读取过来的,读取过来的字典并非python对象数据类型而是string类型。这
- Varchar 对每个英文(ASCII)字符都占用2个字节,对一个汉字也只占用两个字节char 对英文(ASCII)字符占用1个字节,对一个
- 测试环境Python 3.6Win10代码实现#!/usr/bin/env python 3.4.0#-*- encoding:utf-8
- 在用到编辑器时,就会碰到一点,那就是标签的闭合问题,这个问题非常严峻,因为这可能会导致网页显示的整体样式受到破坏。这最近在PJ的functi
- 关于 PHP 的文件操作,我们也将是通过一系列的文章来进行学习。今天我们先学习的是一个很少人使用过,甚至很多人根本不知道的扩展,它与我们日常
- 本文实例讲述了python计算一个序列的平均值的方法。分享给大家供大家参考。具体如下:def average(seq, total=0.0)
- 本文主要给大家介绍了关于webpack中publicPath路径问题的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:output:
- 这里我不讨论 python 的一些有用的库或者框架,只从语言本身,最小支持的情况下谈论这门语言本身。语言的发展都是越来越接近Lisp,这也是
- 1 configparser安装pip3 install configparser2 configparser简介用来读取配置文件的pyth
- //冒泡排序func mpSort(array []int) { for i:=0;i<len(array);i++ {