python isinstance 判断各种类型的小细节
1. 基本语法
isinstance(object, classinfo)
Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. Also return true if classinfo is a type object (new-style class) and object is an object of that type or of a (direct, indirect or virtual) subclass thereof. If object is not a class instance or an object of the given type, the function always returns false. If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted). If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised.
classinfo 处可以是 a class, type, or tuple of classes and types,
如果是 tuple,则满足 tuple 中的任何一个即返回 True
2. 字符串的类型判断。
字符串,分为 str 和 unicode,二者均继承自 basestring
- >>> isinstance(u'3.0', unicode)
- True
- >>> isinstance('3.0', str)
- True
- >>> isinstance(u'3.0', str)
- False
- >>> isinstance(u'3.0', str)
- False
- >>> isinstance(u'3.0', basestring)
- True
- >>> isinstance('3.0', basestring)
- True
3. 数字的类型判断
数字分为 int 和 float,暂未发现二者共同的有效父类。
可以用 (int, float) tuple 来判断是否为数字(int 或 float)
- >>> isinstance('', (int, float))
- False
- >>> isinstance(3.0, (int, float))
- True
- >>> isinstance(3, (int, float))
- True
- >>> isinstance(3.0, float)
- True
- >>> isinstance(3.0, int)
- False
- >>> isinstance(3, float)
- False
- >>> isinstance(3, int)
- True
python isinstance 判断各种类型的小细节的更多相关文章
- Python isinstance判断对象类型
在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: class objA: pass A = objA() B = 'a','v' C = 'a string ...
- Python中请使用isinstance()判断变量类型
一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...
- python准确判断文件类型
判断文件类型在开发中非常常见的需求,怎样才能准确的判断文件类型呢?首先大家想到的是文件的后缀,但是非常遗憾的是这种方法是非常不靠谱的,因为文件的后缀是可以随意更改的,而大家都知道后缀在linux系统下 ...
- python中判断对象类型的函数——isinstance
isinstance是Python中的一个内建函数.是用来判断一个对象的变量类型. isinstance(object, class-or-type-or-tuple) 如果参数object是clas ...
- 【python】【转】python中isinstance判断变量类型用法
来源 http://www.jb51.net/article/15696.htm 在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: 复制代码 代码如下: c ...
- python实例[判断操作系统类型]
参考文献:http://bbs.chinaunix.net/thread-1848086-1-1.html 经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台( ...
- Python正则表达式使用过程中的小细节
今天用Python写了个简单的爬虫程序,抓取虎扑篮球(nba.hupu.com)的首页内容,代码如下: #coding:gb2312 import urllib2, re webpage = urll ...
- python isinstance()判断数据类型
举例: d = (1,2,3) print(isinstance(d,int)) #False print(isinstance(d,tuple)) #True print(isinstance(d, ...
- Python中为什么推荐使用isinstance来进行类型判断?而不是type
转自:http://www.xinxingzhao.com/blog/2016/05/23/python-type-vs-isinstance.html Python在定义变量的时候不用指明具体的的类 ...
随机推荐
- 自己写一个与startWith类似的判断方法
package com.hanqi.lianxi; import java.util.Scanner; public class startWith { //自己顶一个与startWit ...
- Java Swing界面编程(1)
写多了jsp,对于页面式的系统已经写烦了,本人也開始着手于java swing的学习,作为菜鸟,仅想用博客记录下我的swing学习的历程.话不多说,首先開始我的第一个窗体化程序. 下面给出源码: pa ...
- Database and models
Database and models The database Now that we have the Album module set up with controller action met ...
- 常用免费快递查询API对接案例
现在许多电商公司和ERP都会寻找比较适用的集成快递查询接口,减少对接难度,现在整理一下常用的免费快递查询接口,并附上调用案例,如果有觉得不对的地方,望能够一起沟通探讨! 一.快递查询接口 目前有提供免 ...
- 万能的Entry,两个变量的Model/JavaBean再也不用写了!
前言 很多时候传数据需要只含两个变量的Model/JavaBean,但就为了两个变量去写一个Model/JavaBean实在是麻烦,而且类型固定重用性低.比如: 1.网格显示的头像-名称 需要 Str ...
- tomcat服务器使用简介
tomcat服务器的应用与部署:1:下载tomcat服务器可以到http://tomcat.apache.org/下载apache服务器,左侧有各种版本的服务器,可以根据自己的需要下载,如果是是Lin ...
- jquery用on代替bind(),live(),delegate()的方法
Js的功能确实非常强大,奈何我一个php程序员一直在js上没有投入足够的精力去研究,每次遇到不会的都是去百度,以后有时间真的应该买本书系统的学习一下 // Bind $( "#members ...
- [引]ASP.NET MVC 4 Content Map
本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...
- uva - 133 The Dole Queue(成环状态下的循环走步方法)
类型:循环走步 #include <iostream> #include <sstream> #include <cstdio> #include <cstr ...
- 修改BASH的配色
PS1变量简介 PS1是Linux终端用户的一个环境变量,用来说明命令行提示符的设置. \d :#代表日期,格式为weekday month date,例如:"Mon Aug 1" ...