python 关键字 continue 正在将控制权转移到 finally 关键字

python keyword continue is shifting control to finally keyword

我实际上是 python 的新手。在学习它的过程中,我遇到了这段代码。

Python官方文档说遇到continue语句时, 控制将转移到循环的开头,但在这种情况下,它将转移到最终语句并从那里开始执行。这是 python 中的错误还是什么?有人可以向我解释一下吗?谢谢。

def askint():

 while True:

    try:
        val =int(input("pleas enter an  integer  "))
    except:
        print ("it seems like you did'n enter an integer ")
        continue
    else:
        print ("yep that's an integer thank you")
        break

    finally:
        print ('control is now on finally me')  
    print ('i am also getting executed     ')       



askint()

finally 代码总是在 try/except 块中执行。

continue 不会跳过它(否则它会成为 python 中的错误)。

finally子句必须执行不管发生什么,也是如此。

这就是它被称为 finally 的原因:无论您 尝试 成功还是引发 except[=21= 都无关紧要]ion,它总是执行。

这在 documentation for the continue statement 中说明:

When continue passes control out of a try statement with a finally clause, that finally clause is executed before really starting the next loop cycle.

(强调我的)

是的,如果没有 while 循环,您会弹出一个 SyntaxError;因为 continuewhile 中并且 finally 总是有机会完成事情;它在出路时被执行。

finally 总是会执行在一个try/exept不管是什么exception.I认为这个material会帮助你https://docs.python.org/2.5/whatsnew/pep-341.html