Why does this code returns 2? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does this code returns 2?

def func(): try: return 1 finally: return 2 k=func() print(k) there are supposed to be no error in try section, so if it returns anything it should be 1 and 2. or does 2 overwrites the return of 1?

29th Nov 2021, 2:42 AM
Nordo Fin
Nordo Fin - avatar
2 Answers
+ 3
See this answer to a similar question on StackOverflow https://stackoverflow.com/a/19805746
29th Nov 2021, 2:48 AM
XXX
XXX - avatar
+ 2
The finally block is always executed. And a function can only return one object.
29th Nov 2021, 3:47 AM
Simon Sauter
Simon Sauter - avatar