This is a tricky Python try/finally behavior. Let’s go step by step.
Code:
Step 1: Entering the try block
-
Inside try, we hit:
return 1 -
So Python prepares to return 1.
Step 2: But finally always runs
-
Before leaving the function, Python always executes the finally block, no matter what.
-
Here, the finally block has:
return 2
Step 3: Return value override
-
The finally block overrides whatever was going to be returned from try.
-
So the return 1 gets discarded, and the function actually returns 2.
Final Output:
2✅ Explanation: In Python, finally always runs, and if it has a return, it overrides the return in try/except.


0 Comments:
Post a Comment