gpt4 book ai didi

python - Try-Except-Finally 语句的过时书籍描述

转载 作者:太空狗 更新时间:2023-10-29 20:37:29 25 4
gpt4 key购买 nike

我正在关注 Apress,从新手到专业的 Python 入门 这本书。据说:

finally. You can use try/finally if you need to make sure that some code (for example, cleanup code) is executed regardless of whether an exception is raised or not. This code is then put in the finally clause. Note that you cannot have both except clauses and a finally clause in the same try statement—but you can put one inside the other.

我试过这段代码:

def someFunction():
a = None
try:
a = 1 / 0
except ZeroDivisionError, e:
print 'Yesss'
print e
finally:
print 'Cleanup'
del a

if __name__ == '__main__':
someFunction()

...输出是

Yesss
integer division or modulo by zero
Cleanup

在这里,我在同一个try 段中使用了exceptfinally,不是吗?并且代码按预期工作正常。我不太明白书上说的是什么!

有人请澄清。谢谢。

最佳答案

自 python 2.5 以来已修复此问题,并在 documentation 中明确指出

换句话说,你的书不正确/过时了

关于python - Try-Except-Finally 语句的过时书籍描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com