python判断变量类型时,为什么不推荐使用type()方法
2013-07-25 14:19
543 查看
用type()这种判断变量的方法,结果老鸟被鄙视了,不知所以然。
求各位讲解:这个问题出在哪里,怎样判断一个变量的类型才是好方法?
实际上还有一种方法是用isinstance
比如:
isinstance 和 type的区别在于:
区别就是 对于subclass之类的 type就不行事了
你说的老鸟 应该是这个意思吧? [颤抖ing]
http://segmentfault.com/q/1010000000127305
求各位讲解:这个问题出在哪里,怎样判断一个变量的类型才是好方法?
>>> n = 911 >>> type(n) <type 'int'> >>> type(n) is int True
实际上还有一种方法是用isinstance
比如:
a = 111 isinstance(a, int) True
isinstance 和 type的区别在于:
class A: pass class B(A): pass isinstance(A(), A) # returns True type(A()) == A # returns True isinstance(B(), A) # returns True type(B()) == A # returns False
区别就是 对于subclass之类的 type就不行事了
你说的老鸟 应该是这个意思吧? [颤抖ing]
http://segmentfault.com/q/1010000000127305
相关文章推荐
- python判断变量类型时,为什么不推荐使用type()方法
- Python中为什么推荐使用isinstance来进行类型判断?而不是type
- python不使用type(obj) is **来判断变量的类型
- Python中请使用isinstance()判断变量类型
- 【Python】判断一个变量的数据类型的两种方法
- Python中请使用isinstance()判断变量类型
- Python中请使用isinstance()判断变量类型
- Python中使用isinstance()判断变量类型
- Python 判断变量类型用instance而不用type
- Python 继承、多重继承、多态、isinstance判断类型、type()获取变量类型、dir()获取变量的所有属性
- 初窥Python(四)——三种方法判断python变量类型
- Python中请使用isinstance()判断变量类型
- js判断undefined变量类型使用typeof
- python中判断一个变量和类是否定义的方法
- Java基础问题:static方法中为什么不能使用非static的方法或变量
- 巧妙使用CSS媒体查询(Media Queries)和JavaScript判断浏览器设备类型的好方法
- 使用CSS媒体查询(Media Queries)和JavaScript判断浏览器设备类型的方法
- 使用Python进行二进制文件读写的简单方法(推荐)
- 线程系列07,使用lock语句块或Interlocked类型方法保证自增变量的数据同步
- 使用CSS媒体查询和JavaScript判断浏览器设备类型的好方法