ITPub博客

首页 > 应用开发 > IT综合 > python断言的使用

python断言的使用

原创 IT综合 作者:lsq_008 时间:2016-09-14 18:06:55 0 删除 编辑
为了人工抛出一个异常,可以使用断言assert,如下:
#coding=utf-8

def testAssert(path):
import os
assert isinstance(path,str),'Need string'
if os.path.isdir(path) == True:
print path + ' is a dir!'
return path
else:
return "Not dir!"

if __name__ == '__main__':
print testAssert(2)

测试如下:
D:\work\bjmobile\daily_report>python test.py
Traceback (most recent call last):
  File "test.py", line 13, in
    print testAssert(2)
  File "test.py", line 5, in testAssert
    assert isinstance(path,str),'Need string'
AssertionError: Need string

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10972173/viewspace-2125003/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
十余年大型金融及电信系统数据库管理经验,曾服务于中国建设银行、中国移动。对oracle,mysql数据库有深入了解。 擅长python开发,独立开发了开源数据库自动化监控运维平台Power Monitor。

注册时间:2008-02-29

  • 博文量
    335
  • 访问量
    1369103