当前位置:网站首页>断言(assert)的用法
断言(assert)的用法
2022-06-24 19:50:00 【ㄣ知冷煖*】
前言
assert关键字更像是一个假设,如果作者的假设条件成立,则程序正常并且良好的运行,但是如果假设不成立,那么程序就会报错,并且终止运行(这样直接避免了引起更大的错误)。
一、用法
1-0、语法格式
assert 表达式,描述错误的字符串
如果发生异常就返回其后的描述错误的字符串。
1-1、检验传入参数的合法性
def student(student_num):
"""
断言传入的学生数量必须大于0。
"""
assert student_num>0, '学生数量必须大于0!!!'
1-2、如果有多个条件,尽量多使用几个断言
assert student_num>0, '学生数量必须大于0!!!'
assert student_age>0, '学生年龄必须大于0!!!'
# 相比于下边的写法,多写几个断言更容易判断出哪里出了问题。
# assert student_age>0 && student_num>0, '学生数量和年龄必须大于0!!!'
二、缺点
缺点:频繁的调用会极大的影响程序的性能,增加额外的开销。
参考文章:
断言(assert)的用法.
总结
边栏推荐
- Design scheme of authority management of fusion model
- im即时通讯开发应用保活之进程防杀
- The drawableleft of the custom textview in kotlin is displayed in the center together with the text
- Requests Library
- ArcGIS loads free online historical images as the base map (no plug-ins are required)
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
- vim使用命令
- canvas线条的动态效果
- Canvas spiral style animation JS special effect
- Usage of ViewModel and livedata in jetpack
猜你喜欢
wx小程序跳转页面
【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学
Eye gaze estimation using webcam
Ansible及playbook的相关操作
Zed acquisition
Tutorial details | how to edit and set the navigation function in the coolman system?
有趣的checkbox计数器
Time unified system
Paper review: U2 net, u-net composed of u-net
What are the advantages of VR panoramic production? Why is it favored?
随机推荐
Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar
Basic summary of MySQL database knowledge
信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)
Im instant messaging development application keeping alive process anti kill
canvas线条的动态效果
Report on operation mode and future development trend of global and Chinese propenyl isovalerate industry from 2022 to 2028
JPA learning 2 - core annotation, annotation addition, deletion, modification and query, list query result return type, one to many, many to one, many to many
Why do more and more physical stores use VR panorama? What are the advantages?
5G dtu无线通信模块的电力应用
C program design topic 18-19 final exam exercise solutions (Part 2)
Binder mechanism and Aidl communication example
Go crawler framework -colly actual combat (I)
VNC viewer remote connection raspberry pie without display
第三代电力电子半导体:SiC MOSFET学习笔记(五)驱动电源调研
What exactly is Nacos
Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
Opengauss kernel: simple query execution
Go crawler framework -colly actual combat (II) -- Douban top250 crawling
Jar package merging using Apache ant
Common redis commands in Linux system