+ 5

[SOLVED]explain this python code

def foo(): try: return 1 finally: return 2 k = foo() print(k) //prints 2. Why return 1 fails? def foo(): try: return 1 except: return 2 k = foo() print(k) //prints 1 but this returns 1 in this case and try doesn't fail def foo(): try: return 1 except: return 2 finally: return 3 k = foo() print(k) //prints 3 in this case. Can you please explain the concept behind this behaviour?

16th Mar 2021, 5:20 AM
CHANDAN ROY
CHANDAN ROY - avatar
19 Réponses
16th Mar 2021, 7:37 PM
A͢J
A͢J - avatar
+ 4
finally clause is always executed, even if there was other return statements elsewhere (in try..except blocks)
16th Mar 2021, 7:41 AM
visph
visph - avatar
+ 4
CHANDAN ROY Not only in python even in other languages also same thing happens.
16th Mar 2021, 8:03 PM
A͢J
A͢J - avatar
+ 3
CHANDAN ROY Because you have used return statement. Just change return to print in try block then see effect.
16th Mar 2021, 7:32 PM
A͢J
A͢J - avatar
+ 3
CHANDAN ROY I don't know who is disliking your reply.
16th Mar 2021, 7:58 PM
A͢J
A͢J - avatar
+ 3
return in try..except block are only executed if there's no return statement in finally block... but with the computed value of return before executing finally block ^^ https://code.sololearn.com/cSF2e2KEsVXv/?ref=app
16th Mar 2021, 8:11 PM
visph
visph - avatar
+ 2
CHANDAN ROY finally always execute that's why in 1st and 3rd case outputs are 2 and 3 In 2nd case 1 will print because there is no finally and also there is no exception otherwise 2 was printed.
16th Mar 2021, 7:24 PM
A͢J
A͢J - avatar
+ 2
Dreams Hacker If similar question is available and you have search option then why we would like to answer you?
17th Mar 2021, 5:13 PM
A͢J
A͢J - avatar
+ 1
I Am AJ ! Sir, can you please explain this
16th Mar 2021, 7:09 PM
CHANDAN ROY
CHANDAN ROY - avatar
+ 1
Super applis et je viens de créer MA première partie
18th Mar 2021, 2:54 AM
Sorike Danfakha
Sorike Danfakha - avatar
0
I Am AJ ! In first case, why doesn't it print 1 and 2 both. Why return 1 doesn't work?
16th Mar 2021, 7:28 PM
CHANDAN ROY
CHANDAN ROY - avatar
0
I Am AJ ! But if it returns both 1 and 2. When we print k, it should print both, no?
16th Mar 2021, 7:36 PM
CHANDAN ROY
CHANDAN ROY - avatar
0
Okay, I was being dumb. Got it. Thanks for your patience. Your suggestion to Change return to print in try block made me understand this behaviour. Now I understand it as this. since return stops the execution of a code block, it is ignored if there is a finally block present as finally has to be executed at all costs. Python accords more priority to finally block and to make it work, despite code in try block having no exception, since return will stop the execution of code, it is ignored. Thanks a lot!!
16th Mar 2021, 7:49 PM
CHANDAN ROY
CHANDAN ROY - avatar
0
I Am AJ ! There is no way to find out I guess.
16th Mar 2021, 8:07 PM
CHANDAN ROY
CHANDAN ROY - avatar
0
OMR4N Please try to check the behaviour of exception in python. If there is return statement in try block of exception there must be release of all resources. It must be freed or released, that's the purpose of finally block to free the occupied resources, if return is called with in try block resources needs to be freed before return. This was the message I want to convey. DHANANJAY
18th Mar 2021, 5:17 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
- 1
ok ill make it short i am not a expert in python3 programming but i have enough knowledge to answer you. this is a strage code but i like it hehehe. btw, the finally keyword keep in mind it will be executed if there is a error or no. so the interepter executes try the try returns 1 but like i said finnaly will be executed if there is a error or no so the finally will return 2 so thats why.
18th Mar 2021, 3:31 AM
Imrane Aabbou
Imrane Aabbou - avatar
- 2
CHANDAN ROY Your follow up is very good it must call finally while executing, return statement prevents it. There must be guidelines for that or error needs to be appear while executing. DHANANJAY
17th Mar 2021, 3:01 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
- 5
Do you know sololearn community is boring now because there are even no answers on some new question most moderators rate some similar question as same because they don't want to answer them.
17th Mar 2021, 5:11 PM
𝖆𝖊𝖙𝖍𝖊𝖗
𝖆𝖊𝖙𝖍𝖊𝖗 - avatar