这里有两种方法。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. asp.net MVC4 表单 - CheckBox兴趣爱好

    1.Model层定义属性 public class vmUser { public string userName { get; set; } public string sex { get; set ...

  2. document.compatMode属性

    document.compatMode用来判断当前浏览器采用的渲染方式. 官方解释: BackCompat:标准兼容模式关闭.CSS1Compat:标准兼容模式开启. 当document.compat ...

  3. .Net Framework认知

    在托管代码的世界里,应用程序首先被加载到应用程序域(AppDomain)中,然后将应用程序域加载到进程中,一个进程可以包含多个应用程序域,也就是说一个进程可以包含多个应用程序,毕竟应用程序域之间的切换 ...

  4. shanquan2的两年三题系列

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

  5. http://detectmobilebrowsers.com/

    <%@ Page Language="C#" %> <%@ Import Namespace="System.Text.RegularExpressio ...

  6. composer 使用笔记

    使用composer 更新项目比如: composer create-project topthink/think wwwroot dev-master --prefer-dist提示openssl异 ...

  7. Socket网络编程二

    SocketServer socketserver一共有这么几种类型 class socketserver.TCPServer(server_address, RequestHandlerClass, ...

  8. Toast 工具

    public class Utils { private static Toast toast; public static void showtoast(Context context,String ...

  9. ubuntu 12.04 install docker-engine1.12.3

    root@node3:/data/src# cat /etc/issueUbuntu 12.04.4 LTS \n \l   root@node3:/data/src# cat /etc/apt/so ...

  10. Python flask @app.route

    转载自 http://python.jobbole.com/80956/ 下面是Flask主页给我们的第一个例子,我们现在就由它入手,深入理解“@app.route()”是如何工作的.         ...