Pyhton strange function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pyhton strange function

As I know if in a function we have the "return" command the lines after that will be ignored. But in this code: https://code.sololearn.com/c3R3zB6JvW1O I wonder to know why the first return is ignored and the second one works?

30th Apr 2019, 5:28 AM
rahim
rahim - avatar
4 Answers
+ 1
It looks like python is splitting the try catch finally block into seperate part which each can have a return statement. Because the finally block will be called if the try block fails or dosnt fail. So most likely the function will return 1 and then it will overide that 1 with a 2. Further testing is interesting here :)
30th Apr 2019, 5:44 AM
Dragonxiv
Dragonxiv - avatar
0
It is interesting that the try block runs completely and then finally block runs also. :)
30th Apr 2019, 5:53 AM
rahim
rahim - avatar
0
It means that if "return" will be in a try block it dose not return anything until try block evaluates completely and then the last value will be return. Very interesting that try block is in a higher place than return.
30th Apr 2019, 5:59 AM
rahim
rahim - avatar