当前位置:网站首页>断言(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)的用法.
总结
边栏推荐
猜你喜欢
Modstart: embrace new technologies and take the lead in supporting laravel 9.0

wx小程序跳转页面

Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform

Go crawler framework -colly actual combat (II) -- Douban top250 crawling

为什么生命科学企业都在陆续上云?

Unmanned driving: Some Thoughts on multi-sensor fusion

颜色渐变梯度颜色集合

Ten commandments of self-learning in machine learning

Tutorial details | how to edit and set the navigation function in the coolman system?

JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
随机推荐
Paint rounded rectangle
JDBC - database connection
Alternative to log4j
Some examples of MgO operating database in go
从数字化过渡到智能制造
部门新来的00后真是卷王,工作没两年,跳槽到我们公司起薪18K都快接近我了
December 6, 2019 what happens after the browser enters a URL
Collective example
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
Svg line animation background JS effect
【图数据库性能和场景测试利器LDBC SNB】系列一:数据生成器简介 & 应用于GES服务
Requests Library
C program design topic 15-16 final exam exercise solutions (Part 1)
Creative SVG ring clock JS effect
【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
How to delete the entire row with duplicate items in a column of WPS table
Is it so difficult to calculate the REM size of the web page according to the design draft?
ServerSocket and socket connection
Intensive reading of thinking about markdown
JDBC —— 数据库连接