python 判断类型
转自:http://san-yun.iteye.com/blog/1543174 Python可以得到一个对象的类型 ,利用type函数:
>>>lst = [1, 2, 3]
>>>type(lst)
<type 'list'>
不仅如此,还可以利用isinstance函数,来判断一个对象是否是一个已知的类型。
isinstance说明如下:
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for isinstance(x, A)
or isinstance(x, B) or ... (etc.).
其第一个参数为对象,第二个为类型名或类型名的一个列表。其返回值为布尔型。
若对象的类型与参数二的类型相同则返回True。若参数二为一个元组,则若对象类型与元组中类型名之一相同即返回True。
>>>isinstance(lst, list)
Trueisinstance(lst, (int, str, list))
True
>>>isinstance(lst, (int, str, list))
True
这也算Python的强大之一吧!
python 判断类型的更多相关文章
- python判断类型:想知道一个对象(实例或者变量)是什么类型,什么结构的
用type和isinstance 例子: ta={} ta['1']="a" ta={'2':"b"} ta['3']="c" #使用两个函 ...
- python判断类型
方法 isinstance(obj, type) 示例 >>> print isinstance(, int) True >>> print isinstance( ...
- python继承,判断类型,多态
1.python中继承 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init_ ...
- Python序列类型
Python序列类型 序列:字符.列表.元组 所有序列都支持迭代 序列表示索引为非负整数的有序对象集合 字符和元组属于不可变序列,列表可变 1)字符 字符串字面量:把文本放入单引号.双引号或三引号中: ...
- python动态类型
在python中,省去了变量声明的过程,在引用变量时,往往一个简单的赋值语句就同时完成了,声明变量类型,变量定义和关联的过程,那么python的变量到底是怎样完成定义的呢? 动态类型 python使用 ...
- Python判断列表是否已排序的各种方法及其性能分析
目录 Python判断列表是否已排序的各种方法及其性能分析 声明 一. 问题提出 二. 代码实现 2.1 guess 2.2 sorted 2.3 for-loop 2.4 all 2.5 numpy ...
- (转)python 判断数据类型
原文:https://blog.csdn.net/mydriverc2/article/details/78687269 Python 判断数据类型有type和isinstance 基本区别在于: t ...
- 【Python】python动态类型
在python中,省去了变量声明的过程,在引用变量时,往往一个简单的赋值语句就同时完成了,声明变量类型,变量定义和关联的过程,那么python的变量到底是怎样完成定义的呢? 动态类型 python使用 ...
- Python列表类型及常用操作
Python列表类型 1.用途: 存放多个值,可以根据索引存取值 2.定义方式: 在[ ]内用逗号分割开多个任意类型的值 l=['yven','law','lyf'] #l=list(['yven', ...
随机推荐
- c语言memset源码
c语言memset源码 一.用法 void *memset(void *s, int ch, size_t n);作用:将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的 ...
- MVVM模式的3种command总结[2]--RelayCommand
MVVM模式的3种command总结[2]--RelayCommand RelayCommand本来是WPF下面用的一种自定义的command,主要是它用到了事件管理函数,这个SL下面是没有的.不过这 ...
- Codeforces Round #409
第一题很水但是wa了一发,因为没考虑K前面是K的情况 #include<map> #include<set> #include<cmath> #include< ...
- MySQL修改字符集编码
通过修改字符集编码为utf8,彻底解决中文问题. 一. 登录MySQL查看用SHOW VARIABLES LIKE 'character%':下字符集,显示如下: +----------------- ...
- Highcharts 时间序列,可缩放的图表;Highcharts X 轴翻转曲线图;Highcharts 带标记曲线图
Highcharts 时间序列,可缩放的图表 配置 图表 配置可缩放图表. chart.zoomType 指定了用户可以拖放的尺寸,用户可以通过拖动鼠标来放大,可能值是x,y或xy: var char ...
- GPON命令模式
1.添加ont步骤 1.1 查看自动发现的ONT,并记录SN号和PON口 MA5680T(config)#display ont autofind all --------------------- ...
- poj3068
题解: 最小费用最大流 每一次找最短的 代码: #include<cstdio> #include<cmath> #include<cstring> #includ ...
- LeetCode OJ:Min Stack(最小栈问题)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- PentesterLab-From SQL Injection to Shell: PostgreSQL edition
一.打开页面,随便点了几下,返现和From SQL Injection to Shell差不多,直奔主题开始注入 由于PostgreSQL与MySQL不同,几个关注点需要注意下 二.order by下 ...
- LVS模式三:NAT模式
一.NAT模式 NAT(Network Address Translation,网络地址转换).数据包传输过程就是把客户端发来的数据包的IP头的目的地址,在负载均衡器上换成其中一台RS的IP地 ...