这里有两种方法。type 和isinstance

import types

aaa = 0
print type(aaa)
if type(aaa) is types.IntType:
print "the type of aaa is int"
if isinstance(aaa,int):
print "the type of aaa is int" bbb = 'hello'
print type(bbb)
if type(bbb) is types.StringType:
print "the type of bbb is string"
if isinstance(bbb,str):
print "the type of bbb is string" #if the type is NoneType,the isinstance does not work
#we should judge the NoneType like below
#if row is None
#if type(row) is types.NoneType #In my opinion,use the types to judge the type of a param is convinient<span style="font-family:Arial;background-color: rgb(255, 255, 255);">, use the isinstance to judge whether a instance is a type of a class or not</span>

一、isinstance()

在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便。

复制代码代码如下:

 # coding=utf-8

 a = 10

 def b():
pass print isinstance(a,(int,str))
print isinstance(a,(float,str))
print isinstance(b,(str,int)) class c:
pass obj = c() print isinstance(obj,(c,int))

执行结果:

复制代码代码如下:

 True
False
False
True

二、isinstance 和 type的区别

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就不行了,所以,强烈建议不要使用type判断对象类型。

Python 判断变量的类型的更多相关文章

  1. Python如何判断变量的类型

    Python判断变量的类型有两种方法:type() 和 isinstance() 如何使用 对于基本的数据类型两个的效果都一样 type() ip_port = ['219.135.164.245', ...

  2. 在C中判断变量存储类型(字符常量/数组/动态变量)

    在C中判断变量存储类型(字符常量/数组/动态变量) 在chinaunix论坛上有人问到关于变量存府类型的问题,我觉得可以写个测试代码加深大家对内存使用和布局的理解.下面我把原问题及处理办法贴出来,限供 ...

  3. python 判断变量是否存在 防止报错

    Python判断变量是否存在 方法一:使用try: ... except NameError: .... try: var except NameError: var_exists = False e ...

  4. 【Python】Python—判断变量的基本类型

    type() >>> type(123)==type(456) True >>> type(123)==int True >>> type('ab ...

  5. python中判断变量的类型

    python的数据类型有:数字(int).浮点(float).字符串(str),列表(list).元组(tuple).字典(dict).集合(set) 一般通过以下方法进行判断: 1.isinstan ...

  6. Python的变量以及类型

    1.程序是用来处理数据的,变量就是用来存储数据的  num1 = 100 2.为了更充分的利用内存空间以及更有效率的管理内存,变量是有不同的类型 3.怎样知道一个变量的类型呢? 3.1 在python ...

  7. golang 如何判断变量的类型

    本文介绍两种用于判断变量类型的方式. 方法一 package main import ( "fmt" ) func main() { v1 := "123456" ...

  8. Python之变量以及类型

    为了更充分的利用内存空间以及更有效率的管理内存,变量是有不同的类型的,如下所示: 怎样知道一个变量的类型呢? 在python中,只要定义了一个变量,而且它有数据,那么它的类型就已经确定了,不需要咱们开 ...

  9. Python判断输入字符类型

    """从键盘上输入 一个字符,判断其字符类型.""" while True: char = input("请输入需要判断的字符:& ...

随机推荐

  1. Python:基本语法1

    I.Python中的转义符注意情况 如果'本身是一个字符,则可将其用" "括起来: 如果字符串内部既有',又有",则可用转义字符\,比如: 'I\'m\"OK\ ...

  2. 高可用mysql之MHA的原理

    MHA 如何工作的? MHA是如何工作的? ============================================================================== ...

  3. 安卓app开发笔记

    移动app应用开发也是信息技术课程科技创新的范畴,所以在个人开发app时候记录一些笔记,可能会很乱,所以选择按点来写. 首先是一些入门的资料,有很多需要自己学习的 https://www.oschin ...

  4. Dom4j解析xml文件

    dom4j是一个Java的XML API,类似于jdom,用来读取的XML文件,由于它是将文件解析完存放在内存当中的,所以不适合解析大的XML文件,但就方便性和性能方面,一定程度要优于JDK中Domc ...

  5. shanquan2的两年三题系列

    好像只有2个月就退役啦 不管了,先说一下哪三题:多点求值.lcm.替罪羊树(bzoj3065) [upd0]2016.3.29 多点求值A掉啦,myy卡常数sxbk(不是说好的是shanquan2出的 ...

  6. 完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法

    完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法   HTML5的语义化标签以及属性,可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页 ...

  7. windows7 gvim 配置(好用)

    http://blog.csdn.net/anders_zhuo/article/details/8949003

  8. PHP语言中使用JSON和将json还原成数组

    从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码. 一.json_encode() 1 2 3 4 <?php $arr = ...

  9. 将MongoDB服务加入随机启动

    将MongoDB服务加入随机启动 vi /etc/rc.local 使用vi编辑器打开配置文件,并在其中加入下面一行代码 /usr/local/mongodb/bin/mongod -dbpath=/ ...

  10. BZOJ 4184: shallot

    Description 在某时刻加入或删除一个点,问每个时刻的集合中能异或出来的最大值是多少. Sol 线段树+按时间分治+线性基. 按时间分治可以用 \(logn\) 的时间来换取不进行删除的操作. ...