python区分不同数据类型的方法
作者:郭雪原 时间:2022-03-30 23:37:41
python怎么区分不同数据类型?
Python判断变量的数据类型的两种方法
一、Python中的数据类型有数字、字符串,列表、元组、字典、集合等。有两种方法判断一个变量的数据类型
1、isinstance(变量名,类型)
isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
isinstance() 方法的语法:
isinstance(object, classinfo)
2、通过与其他已知类型的常量进行对比
扩展学习:
python 判断数据类型
基本区别在于:
type():不会认为子类是父类
isinstance():会认为子类是父类类型
class Color(object):
pass
class Red(Color):
pass
print type(Color()) == Color
print type(Red()) == Color
print isinstance(Red(),Color)
执行结果如下:
D:\software\Python2.7.13\python.exe C:/Users/Administrator/PycharmProjects/PythonStudy/test.py
True
False
True
来源:https://www.cnblogs.com/guoxueyuan/p/9151678.html
标签:python,数据类型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
np.random.seed() 的使用详解
2021-04-08 23:54:57
![](https://img.aspxhome.com/file/2023/0/117430_0s.png)
Windows 安装 Anaconda3+PyCharm的方法步骤
2023-05-21 07:40:53
![](https://img.aspxhome.com/file/2023/8/107998_0s.png)
Scrapy项目实战之爬取某社区用户详情
2022-04-16 11:36:31
在Python中使用__slots__方法的详细教程
2022-10-30 09:46:10
对Python 多线程统计所有csv文件的行数方法详解
2021-09-09 04:56:44
Selenium之模拟登录铁路12306的示例代码
2022-01-22 17:06:27
![](https://img.aspxhome.com/file/2023/7/92077_0s.png)
Python 可爱的大小写
2021-05-15 12:34:18
eWebEditor不支持IE,IE8,IE7,火狐,遨游的解决方法
2011-06-06 07:57:00
JSP学生信息管理系统设计
2023-07-13 03:37:30
详谈javascript异步编程
2023-08-23 17:49:53
python 下划线的多种应用场景总结
2021-12-19 10:51:54
打造设计你自己的字体
2007-12-12 13:16:00
![](https://img.aspxhome.com/file/UploadPic/200712/12/20071212132556860s.jpg)
Python模板的使用详细讲解
2022-03-20 13:24:08
![](https://img.aspxhome.com/file/2023/7/103237_0s.png)
pytest-fixture简介及其用法讲解
2023-02-13 19:27:25
Python实现随机漫步功能
2021-02-05 20:44:45
![](https://img.aspxhome.com/file/2023/3/117603_0s.png)
UI自动化定位常用实现方法代码示例
2022-09-22 09:47:13
教你快速掌握两个SQL Server的维护技巧
2009-01-15 13:31:00
Python 合并拼接字符串的方法
2022-05-26 08:58:08
Python中TypeError:unhashable type:'dict'错误的解决办法
2022-05-09 19:57:41
![](https://img.aspxhome.com/file/2023/0/101590_0s.png)
Python判断一个文件夹内哪些文件是图片的实例
2023-08-18 01:22:44