+ 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?
19 ответов
+ 8
CHANDAN ROY
Here is complete explanation
https://code.sololearn.com/c3R4fUv1L5HE/?ref=app
+ 4
finally clause is always executed, even if there was other return statements elsewhere (in try..except blocks)
+ 4
CHANDAN ROY
Not only in python even in other languages also same thing happens.
+ 3
CHANDAN ROY
Because you have used return statement. Just change return to print in try block then see effect.
+ 3
CHANDAN ROY
I don't know who is disliking your reply.
+ 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
+ 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.
+ 2
Dreams Hacker
If similar question is available and you have search option then why we would like to answer you?
+ 1
I Am AJ ! Sir, can you please explain this
+ 1
Super applis et je viens de créer MA première partie
0
I Am AJ ! In first case, why doesn't it print 1 and 2 both. Why return 1 doesn't work?
0
I Am AJ ! But if it returns both 1 and 2. When we print k, it should print both, no?
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!!
0
I Am AJ ! There is no way to find out I guess.
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
- 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.
- 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
- 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.