Skip to content

Commit 35d3581

Browse files
committed
error3
1 parent 3b7d033 commit 35d3581

4 files changed

Lines changed: 114 additions & 1 deletion

File tree

217.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,6 @@ finally子句,一听这个名字,就感觉它是做善后工作的。的确
224224

225225
------
226226

227-
[总目录](./index.md)   |   [上节:错误和异常(1)](./217.md)   |   [下节:错误和异常(3)](./218.md)
227+
[总目录](./index.md)   |   [上节:错误和异常(1)](./216.md)   |   [下节:错误和异常(3)](./218.md)
228228

229229
如果你认为有必要打赏我,请通过支付宝:**qiwsir@126.com**,不胜感激。

218.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
>凡事谦虚、温柔、忍耐,用爱心互相宽容,用平和彼此联络,竭力保守圣灵所赐合而为一的心。(EPHESIANS 4:2-3)
2+
3+
#错误和异常(3)
4+
5+
按照一般的学习思路,掌握了前两节内容,已经足够编程所需了。但是,我还想再多一步,还是因为本教程的读者是要from beginner to master。
6+
7+
##assert
8+
9+
>>> assert 1==1
10+
>>> assert 1==0
11+
Traceback (most recent call last):
12+
File "<stdin>", line 1, in <module>
13+
AssertionError
14+
15+
从上面的举例中可以基本了解了assert的特点。
16+
17+
assert,翻译过来是“断言”之意。assert是一句等价于布尔真的判定,发生异常就意味着表达式为假。
18+
19+
assert的应用情景就有点像汉语的意思一样,当程序运行到某个节点的时候,就断定某个变量的值必然是什么,或者对象必然拥有某个属性等,简单说就是断定什么东西必然是什么,如果不是,就抛出错误。
20+
21+
#!/usr/bin/env python
22+
# coding=utf-8
23+
24+
class Account(object):
25+
def __init__(self, number):
26+
self.number = number
27+
self.balance = 0
28+
29+
def deposit(self, amount):
30+
assert amount > 0
31+
self.balance += balance
32+
33+
def withdraw(self, amount):
34+
assert amount > 0
35+
if amount <= self.balance:
36+
self.balance -= amount
37+
else:
38+
print "balance is not enough."
39+
40+
上面的程序中,deposit()和withdraw()方法的参数amount值必须是大于零的,这里就用断言,如果不满足条件就会报错。比如这样来运行:
41+
42+
if __name__ == "__main__":
43+
a = Account(1000)
44+
a.deposit(-10)
45+
46+
出现的结果是:
47+
48+
$ python 21801.py
49+
Traceback (most recent call last):
50+
File "21801.py", line 22, in <module>
51+
a.deposit(-10)
52+
File "21801.py", line 10, in deposit
53+
assert amount > 0
54+
AssertionError
55+
56+
这就是断言assert的引用。什么是使用断言的最佳时机?有文章做了总结:
57+
58+
如果没有特别的目的,断言应该用于如下情况:
59+
60+
- 防御性的编程
61+
- 运行时对程序逻辑的检测
62+
- 合约性检查(比如前置条件,后置条件)
63+
- 程序中的常量
64+
- 检查文档
65+
66+
(上述要点来自:[Python 使用断言的最佳时机](http://www.oschina.net/translate/when-to-use-assert) )
67+
68+
不论是否理解,可以先看看,请牢记,在具体开发过程中,有时间就回来看看本教程,不断加深对这些概念的理解,这也是master的成就之法。
69+
70+
最后,引用危机百科中对“异常处理”词条的说明,作为对“错误和异常”部分的总结(有所删改):
71+
72+
>异常处理,是编程语言或计算机硬件里的一种机制,用于处理软件或信息系统中出现的异常状况(即超出程序正常执行流程的某些特殊条件)。
73+
74+
>各种编程语言在处理异常方面具有非常显著的不同点(错误检测与异常处理区别在于:错误检测是在正常的程序流中,处理不可预见问题的代码,例如一个调用操作未能成功结束)。某些编程语言有这样的函数:当输入存在非法数据时不能被安全地调用,或者返回值不能与异常进行有效的区别。例如,C语言中的atoi函数(ASCII串到整数的转换)在输入非法时可以返回0。在这种情况下编程者需要另外进行错误检测(可能通过某些辅助全局变量如C的errno),或进行输入检验(如通过正则表达式),或者共同使用这两种方法。
75+
76+
>通过异常处理,我们可以对用户在程序中的非法输入进行控制和提示,以防程序崩溃。
77+
78+
>从进程的视角,硬件中断相当于可恢复异常,虽然中断一般与程序流本身无关。
79+
80+
>从子程序编程者的视角,异常是很有用的一种机制,用于通知外界该子程序不能正常执行。如输入的数据无效(例如除数是0),或所需资源不可用(例如文件丢失)。如果系统没有异常机制,则编程者需要用返回值来标示发生了哪些错误。
81+
82+
>一段代码是异常安全的,如果这段代码运行时的失败不会产生有害后果,如内存泄露、存储数据混淆、或无效的输出。
83+
84+
>Python语言对异常处理机制是非常普遍深入的,所以想写出不含try, except的程序非常困难。
85+
86+
------
87+
88+
[总目录](./index.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[上节:错误和异常(2)](./217.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[下节:模块(1)](./219.md)
89+
90+
如果你认为有必要打赏我,请通过支付宝:**qiwsir@126.com**,不胜感激。

2code/21801.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class Account(object):
5+
def __init__(self, number):
6+
self.number = number
7+
self.balance = 0
8+
9+
def deposit(self, amount):
10+
assert amount > 0
11+
self.balance += balance
12+
13+
def withdraw(self, amount):
14+
assert amount > 0
15+
if amount <= self.balance:
16+
self.balance -= amount
17+
else:
18+
print "balance is not enough."
19+
20+
if __name__ == "__main__":
21+
a = Account(1000)
22+
a.deposit(-10)

index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
1. [错误和异常(1)](./216.md)==>什么是错误和异常,常见异常类型,处理异常(try...except...)
7777
2. [错误和异常(2)](./217.md)==>处理多个异常,else子句,finally子句
78+
3. [错误和异常(3)](./218.md)==>assert断言,异常小结
7879

7980
##第陆章 模块
8081

0 commit comments

Comments
 (0)