码迷,mamicode.com
首页 > 其他好文 > 详细

while_else, for_else 以及try_exception_else_finally

时间:2018-12-16 23:23:15      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:state   The   例子   关于   name   margin   int   ...   new   

     在python中的while, for和try是有else分支的. 关于while的else分支描述如下

The while statement is used for repeated execution as long as an expression is true:

while_stmt ::=  "while" expression ":" suite
                ["else" ":" suite]
This repeatedly tests the expression and, if it is true, executes the first suite; if 
the expression is false (which may be the first time it is tested) the suite of the else
clause, if present, is executed and the loop terminates. A break statement executed in the first suite terminates the loop without executing the
else clause’s suite. A continue statement executed in the first suite skips the rest of
the suite and goes back to testing the expression.

总结一句话就是当while或者for正常结束循环,那么else分支就会被执行.即当expression条件不再满足,

如果在执行循环过程中由于break, return等退出,那么else分支不被执行.

对于try... exception...else 当try分支执行后没有产生exception,那么执行else分支.如果有finally分支,则

无论有没有exception,最后都会执行finally分支.但是前提是finally得有except X/except处理了exception,finally

才会被执行. 例子如下: file_bname是一个bytes类型的文件名字, file_name是字符串形式的

(1)当没有出现except时,else和finally被执行
>>> try: ... file_bname.decode(gb2312) ... except TypeError: ... print(hello) ... else: ... print(else) ... finally: ... print(finally) ... 迈瑞医疗(300760)_利润表.xls else finally

(2)当有except的时候,else没有被执行, finally被执行
>>> file_name.decode(gb2312) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: str object has no attribute decode >>> try: ... file_name.decode(gb2312) ... except AttributeError: ... print(Error) ... else: ... print(else) ... finally: ... print(finally) ... Error finally

(3)else前面必须跟着except X或者except
>>> try: ... file_name.decode(gb2312) ... else: File "<stdin>", line 3 else: ^ SyntaxError: invalid syntax
(4) 如果except没有得到处理,即使有finally,也没有被执行
>>> try: ... file_name.decode(gb2312) ... finally: ... print(finally) ... finally Traceback (most recent call last): File "<stdin>", line 2, in <module> AttributeError: str object has no attribute decode >>>

 

 

The while statement is used for repeated execution as long as an expression is true:

while_stmt ::=  "while" expression ":" suite
                ["else" ":" suite]

This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.

break statement executed in the first suite terminates the loop without executing the elseclause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.

while_else, for_else 以及try_exception_else_finally

标签:state   The   例子   关于   name   margin   int   ...   new   

原文地址:https://www.cnblogs.com/zmiao/p/10128148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!