试试......终于比我想象的更强大。 [英] try...finally is more powerful than I thought.

查看:52
本文介绍了试试......终于比我想象的更强大。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def res():

试试:

a = 1

返回

终于:

打印我能到达这里吗?


res()


输出我能到达这里吗?


我不能说为什么我真的没想到这一点,控制流程是一个很好的因为函数不是很好我真的回到了返回

语句但是在finally:块中执行了该位然后

返回。我想:)


话虽如此,我非常喜欢它。这是如何在内部工作的?当尝试代码块超出范围时,是否最终执行了
?这个

会在回归或异常期间发生,这可以解释魔法。


Brian

def res():
try:
a = 1
return
finally:
print "do I get here?"

res()

outputs "do I get here?"

I can''t say why I didn''t really expect this, the control flow is a
little wierd as the function isn''t really returning at the "return"
statement but executing the bit in the finally: block and then
returning. I think :)

That being said, I like it a lot. How is this working internally? Does
the finally get executed when try code block goes out of scope? This
would happen during a return or an exception which could explain the magic.

Brian

推荐答案

Brian Kelley写道:
Brian Kelley wrote:

def res():
试试:
a = 1
返回
终于:
打印我能到这里来吗?

res()

输出我能到达这里吗? br />
我不能说为什么我没想到这一点,控制流程很奇怪,因为函数并没有真正回到返回
语句但是在finally:block中执行该位然后返回
。我认为:)

def res():
try:
a = 1
return
finally:
print "do I get here?"

res()

outputs "do I get here?"

I can''t say why I didn''t really expect this, the control flow is a
little wierd as the function isn''t really returning at the "return"
statement but executing the bit in the finally: block and then
returning. I think :)




这些结果也可能是有意义的:



These results might also be of interest:

def res():
....尝试:

....返回1

....最后:

....返回2

.... res()
2 def res():
....尝试:

....返回1

....最后:

....返回

... .res res()
def res():
....试试:

....返回1

....终于:

....传递

.... res()
def res(): .... try:
.... return 1
.... finally:
.... return 2
.... res() 2 def res(): .... try:
.... return 1
.... finally:
.... return
.... res()
def res(): .... try:
.... return 1
.... finally:
.... pass
.... res()



1


-Peter


1

-Peter


>> def res():
>> def res():
尝试:
a = 1
返回
最后:
打印我做到了吗?"

res()

输出我到这儿了吗?
try:
a = 1
return
finally:
print "do I get here?"

res()

outputs "do I get here?"
这些结果也可能感兴趣:
def res():...尝试:
...返回1
...终于:
...返回2
。 .. res()2 def res():...试试:
...返回1
...终于:
...返回
... res ()
def res():...试试:
...返回1
...终于:
...传递
... res ()1
These results might also be of interest:
def res(): ... try:
... return 1
... finally:
... return 2
... res() 2 def res(): ... try:
... return 1
... finally:
... return
... res()
def res(): ... try:
... return 1
... finally:
... pass
... res() 1





def res():
....打印1

....尝试:

....打印2

....返回3

....打印4

....最后:

....打印5

.... res()
def res(): .... print 1
.... try:
.... print 2
.... return 3
.... print 4
.... finally:
.... print 5
.... res()

< br / >
1

2

5

3

有趣。


Lee Harr写道:
Lee Harr wrote:
def res():... print 1
...试试:
...打印2
...返回3
...打印4
...终于:
。 ..打印5
... res()
def res(): ... print 1
... try:
... print 2
... return 3
... print 4
... finally:
... print 5
... res()


1
2
5

有趣。


1
2
5
3

interesting.




为什么?它只是证明了最终。工作,并且

在遇到return语句时执行。


(我的例子的目的是表明最终可以

实际上覆盖原本会返回的价值

返回。我认为这很有趣,但不幸的是

我不确定你的例子在前两个中添加了什么帖子。

也许我只是错过了这一点......)


-Peter



Why? It just proves that "finally" works, and
executes when the return statement is encountered.

(The point of my examples was to show that finally can
actually override the value that would otherwise have been
returned. I thought that was interesting, but unfortunately
I''m not sure what your example adds to the previous two posts.
Maybe I''m just missing the point...)

-Peter


这篇关于试试......终于比我想象的更强大。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆