Skip to content

Commit 7e30f63

Browse files
committed
p3
1 parent 0172fb5 commit 7e30f63

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

218.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22
33
#错误和异常(3)
44

5-
按照一般的学习思路,掌握了前两节内容,已经足够编程所需了。但是,我还想再多一步,还是因为本教程的读者是要from beginner to master。
5+
###assert
66

7-
##assert
7+
从代码中理解`assert`
88

99
>>> assert 1==1
1010
>>> assert 1==0
1111
Traceback (most recent call last):
1212
File "<stdin>", line 1, in <module>
1313
AssertionError
1414

15-
从上面的举例中可以基本了解了assert的特点
15+
assert,翻译过来是“断言”之意。`assert`是一句等价于布尔真的判定,发生异常就意味着表达式为假
1616

17-
assert,翻译过来是“断言”之意。assert是一句等价于布尔真的判定,发生异常就意味着表达式为假。
18-
19-
assert的应用情景就有点像汉语的意思一样,当程序运行到某个节点的时候,就断定某个变量的值必然是什么,或者对象必然拥有某个属性等,简单说就是断定什么东西必然是什么,如果不是,就抛出错误。
17+
`assert`的应用情景就有点像汉语的意思一样,当程序运行到某个节点的时候,就断定某个变量的值必然是什么,或者对象必然拥有某个属性等,简单说就是断定什么东西必然是什么,如果不是,就抛出异常。
2018

2119
#!/usr/bin/env python
2220
# coding=utf-8
@@ -35,9 +33,9 @@ assert的应用情景就有点像汉语的意思一样,当程序运行到某
3533
if amount <= self.balance:
3634
self.balance -= amount
3735
else:
38-
print "balance is not enough."
36+
print "balance is not enough." #Python 3: print("balance is not enough.")
3937

40-
上面的程序中,deposit()和withdraw()方法的参数amount值必须是大于零的,这里就用断言,如果不满足条件就会报错。比如这样来运行:
38+
上面的程序中,`deposit()``withdraw()`方法的参数`amount`必须是大于零的,这里就用断言,如果不满足条件就会报错。比如这样来运行:
4139

4240
if __name__ == "__main__":
4341
a = Account(1000)

0 commit comments

Comments
 (0)