+ 2
How strong is the "finally" keyword
Consider this code: def func() : try : return 1 # we expect the function to #end here # since it is a return #statement finally : return 2 # but we also know that #finally gets # executed no matter what print(func()) # it outputs 2, so can anyone explain what exactly happened here, please?
2 Answers
+ 3
Since there is a finally, the return within the try skips to the finally. Once finished, the try return would happen, but you all ready returned skipping that code. Your best bet is to set a return variable to 2 before the try. Set it to 1 in the try and let the try exit. In the finally, return the variable.
+ 3
oh Thanks a lot Sir ! @John