+ 7

[SOLVED]Python Question in challenge

def foo(): try: return 1 finally: return 2 k = foo() print(k) after entering try it should return 1 ,but output is 2,help....How code is executed??????

6th Nov 2021, 3:17 AM
Prabhas Koya
8 Answers
+ 9
The Python docs state: When a return, break or continue statement is executed in the try suite of a try…finally statement, the finally clause is also executed ‘on the way out.’ The return value of a function is determined by the last return statement executed. Since the finally clause always executes, a return statement executed in the finally clause will always be the last one executed: https://stackoverflow.com/questions/11164144/weird-try-except-else-finally-behavior-with-return-statements/11164157 https://www.sololearn.com/Discuss/2191347/?ref=app
6th Nov 2021, 7:30 AM
Prabhas Koya
+ 5
Interesting challenge question which I would have gotten wrong also. try, except, finally The tutorial states that the finally statement will always be evaluated. So it seems that the finally statement is either assessed before the try return statement, or it takes precedence over all preceding return statements. I hope you get a better answer than mine as I would also like to know the background logic. Google search, here I come
6th Nov 2021, 3:46 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
def foo(): try: return 1 finally: print(8) k = foo() print(k) This prints 8 1 What's happening
6th Nov 2021, 3:44 AM
Prabhas Koya
+ 3
Prabhas Koya As I said finally block always execute so if there is return then doesn't matter what returned in try block, only finally block returned value will be print because return will stop further execution. I have explained you many examples and also you can see the results.
6th Nov 2021, 7:07 AM
A͢J
A͢J - avatar
+ 2
Prabhas Koya return is used to stop further execution of function. As you can see in your first example there is return in finally block so you will get this returned value. In your second example there is no return in finally block, but there is print function which print 8 but there is return in try block so after printing 8, 1 will be return to calling function. So 1 will be also print. Other examples are explained in my shared code. Remember if we have return in both try and finally then return inside finally will be return to the function.
6th Nov 2021, 5:36 AM
A͢J
A͢J - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ * Remember if we have return in both try and finally then return inside finally will be return to the function. * How the interpreter evaluates this code?? After entering the try block As it encoutered the return statement it should return something instead it went to finally block , and if there is a return it returned without moving further and if there is no return statement then the return statement of try block is executed, Rik Wittkopp has mentioned some logic and I guess it's something that's happening so I want to know more about it....
6th Nov 2021, 6:54 AM
Prabhas Koya
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Can you explain the underlying code mechanism.
6th Nov 2021, 4:11 AM
Prabhas Koya