This article is based on the discussion in this page: http://stackoverflow.com/questions/707674/how-to-compare-type-of-an-object-in-python
From this page, it has the following code sample.
# check if x is a regular string
type(x) == str
# check if x is either a regular string or a unicode string
type(x) in [str, unicode]
# alternatively:
isinstance(x, basestring)
# check if x is an integer
type(x) == int
# check if x is a NoneType
x is None
Also , the author commented that
S.Lott 写道
First, avoid all type comparisons. They're very, very rarely necessary. Sometimes, they help to check parameter types in a function -- even that's rare. Wrong type data will raise an exception, and that's all you'll ever need.
However, if the type comparison is what you really wants, (they must be some case when it is needed), you can do as above.
I am attaching the example that I am having for check the type of parameter to a certain function.
def check(n, p, a=None):
"""
:param n: the # of instance to start
:param p: the path to the Command/Script to start
:param a: this is optional argument,
"""
assert(n is not None and n > 0)
assert(p is not None and p != "")
assert(a is None or a != "")
if (not isinstance(n, int)): raise TypeError("Argument 'n' expect integer")
if (n is None or n <= 0): raise ValueError("Argument 'n' invalid");
if (a is not None):
if (type(a) != StringType): raise TypeError("Argument 'a': expect string")
if (type(a) is not StringType): raise TypeError("Argument 'a': expect string")
if (type(a) != str): raise TypeError("Argument 'a': expect string")
if (type(a) is not str): raise TypeError("Argument 'a': expect string")
# while this is wrong, because is is like identity equal operator
# it is like
# a is b
# is equivalent
# id(a) == id(b)
#
# if (a is not str): raise TypeError("Argument 'a': expect string")
if (not isinstance(a, str)): raise TypeError("Argument 'a': expect string")
if (not (type(a) in [str, unicode])): raise TypeError("Argument 'a': expect string")
pass
分享到:
相关推荐
A Comparison of C, MATLAB, and Python as Teaching Languages in Engineering Hans Fangohr University of Southampton, Southampton SO17 1BJ, UK
资源分类:Python库 所属语言:Python 资源全名:string_comparison-1.0.2-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
python pandas 官网文档 2000多页 目录包括: 1-What's New 2-Installation 3-Contributing to pandas 4-Package overview 【*】5-10 Minutes to pandas 【*】6-Tutorials 【*】7-Cookbook 【*】8-Intro to Data ...
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...
关于Python3中Asyncio库的详细介绍与示例: You’ll learn why Asyncio offers a safer alternative to preemptive multitasking (threading) and how this API provides a simple way to support thousands of ...
Try It Out: Searching for Files of a Particular Type 181 Try It Out: Refining a Search 183 Working with Regular Expressions and the re Module 184 Try It Out: Fun with Regular Expressions 186 Try ...
python_screenshot_comparison_tool
Python Mock库比较这是如何使用流行的Python模拟库和框架完成一些基本任务的并排比较。 该存储库是用于比较的Sphinx源。 您可以在查看比较本身。
arch arch提供了Univariate volatility模型,Bootstrapping和Multiple comparison procedures
python dwg_measurements_comparison4.py [8] 其中,8代表,判定两图尺寸为同一尺寸的最大距离, 单位:米(图上单位)。自己决定具体是多少。 注意: 启动此代码后,首先要在cad软件中打开 对比图, 当该图读完后,...
this is a comparison of db2 vs oracle
fandango_score_comparison.csv 。python 机器学习,电影评分表。
英文comparisonpaper范例,便于进行comparison paper的练习书写
命令行程序的python脚本,当给定两个图像的URL时,它们可以获取和比较图像,并以0-100的比例对其进行评分。 该脚本需要满足以下先决条件: #1. Python 2.7 所需的Python库是#2。 PIL,#3。 numpy,#4。 scipy,...
Python_multiprocessing_4_效率对比_multithreading,_multiprocessing_co
Hex Comparison 是一款二进制文件比较与十六进制编辑工具。它可以帮助你使用二进制格式来比较两个文件。主要功能: 1.允许快速比较两个文件。 2.快速查找每一处不匹配的数据。 3.使用自定义颜色来显示不匹配数据。 4...
Coding in Python minimizes development time and increases productivity in comparison to other languages. Clean, maintainable code is easy to both read and write using Python's clear, concise syntax. ...
Comparison of VHDL Verilog and SystemVerilog VHDL Verilog and SystemVerilog的比较
资源分类:Python库 所属语言:Python 资源全名:streamlit_image_comparison-0.0.2-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059