当前位置:网站首页>断言(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)的用法.
总结
边栏推荐
- Use of JMeter
- svg+js键盘控制路径
- Analysis report on operation pattern and supply and demand situation of global and Chinese cyano ketoprofen industry from 2022 to 2028
- A small program written this week
- Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
- Common redis commands in Linux system
- D does not require opapply() as a domain
- The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
- What exactly is Nacos
- Go crawler framework -colly actual combat (IV) -- Zhihu answer crawl (I)
猜你喜欢

【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学

iNFTnews | 国内NFT发展仅限于数字藏品吗?

有趣的checkbox计数器

在滴滴和字节跳动干了 5年软件测试,太真实…

The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply

时间统一系统

Dynamic effect of canvas lines

What exactly is Nacos

Adding, deleting, modifying and checking in low build code

【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
随机推荐
Time unified system
Collective例子
Analysis report on development trend and investment forecast of global and Chinese D-leucine industry from 2022 to 2028
Report on operation pattern and future prospect of global and Chinese propyl isovalerate industry from 2022 to 2028
Wx applet jump page
Apk slimming compression experience
Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)
中低速航空航天电子总线概述
What exactly is Nacos
Use of JMeter easynmon
Use of navigation and navigationui
Transition from digitalization to intelligent manufacturing
C程序设计专题 18-19年期末考试习题解答(下)
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
Analysis report on development mode and investment direction of sodium lauriminodipropionate in the world and China 2022 ~ 2028
UE4 WebBrowser chart cannot display problems
Go crawler framework -colly actual combat (I)
Unmanned driving: Some Thoughts on multi-sensor fusion
JDBC —— 数据库连接