+ 5
Why don't this return 1?
def f(): try: return 1 finally: return 2 f()
6 Respostas
+ 8
If a function returns something, it means that the return value is saved somewhere and the control flow jumps back to the piece of code where the function was called from. However, here the return statement is embedded in a try block and the function can't just jump back, but the finally block has to be evaluated first. Within the finally block, there is a different return value so this is what will be returned.
+ 7
When an exception handling `try` block has a `finally` block, the `finally` block is executed despite there was any or no exception captured.
Hth, cmiiw
+ 2
Because the 'finally' block returns 2. And it overrides the 1 that returned within the 'try' block.
+ 2
Thanks! Anna
+ 2
Thanks! Kosala
+ 1
Thanks! Ipang