python不使用type(obj) is **来判断变量的类型
来源:互联网 发布:java开发简历项目描述 编辑:程序博客网 时间:2024/06/04 20:03
有时我想判断一个变量的type是不是我想要的那个,该怎么办呢?
用type(obj) is type当然可以
在这里我们还有个办法,用isinstance(obj,type),举个栗子:
import collectionsa=[1,1,2,2,3,3,3]b=collections.Counter(a) #Counter({3: 3, 1: 2, 2: 2})type(b) #<class 'collections.Counter'>isinstance(b,collections.Counter) #True
既然有了type()来判断类型,为什么还有isinstance()呢?
一个明显的区别是在判断子类。
type()不会认为子类是一种父类类型。
isinstance()会认为子类是一种父类类型。
引自:
http://www.pythontab.com/html/2013/pythonjichu_0827/549.html
0 0
- python不使用type(obj) is **来判断变量的类型
- python判断变量类型时,为什么不推荐使用type()方法
- python判断变量类型时,为什么不推荐使用type()方法
- Python 继承、多重继承、多态、isinstance判断类型、type()获取变量类型、dir()获取变量的所有属性
- Python中使用isinstance()判断变量类型
- python 判断变量类型
- Python 判断变量类型
- Python判断变量类型
- python判断变量类型
- python判断变量类型
- IS 函数判断变量类型
- python3怎么判断一个变量的类型(isinstance,type)
- Python—判断变量的基本类型
- jquery $.type来实现类型判断
- 飘逸的python - 类型判断type与isinstance的区别
- Python的type 还是要靠isinstance判断类型
- 扩展typeof来判断js变量的类型
- Logstash type来标记事件类型,通过type判断
- Spring MVC 页面渲染( render view )
- HDU 1671 Phone List (字典树)
- .net中ddd设计模式之domainServices与redis构建redisServices的讨论
- 普通二叉树转二叉链表(孩子兄弟表示法)
- 文件上传限制
- python不使用type(obj) is **来判断变量的类型
- shell实现ftp命令示例
- 网站移动端自适应匹配搜索引擎优化
- yii框架之简单的验证码实现
- Codeforces 513G2 Inversions problem (dp)
- iOS 支付 [支付宝、银联、微信]
- Android输入法开发之Android SDK Sample—SoftKeyboard
- Kafka - SQL 引擎分享
- shell 脚本