Code Explanation:
๐น 1. Function Definition
def func():
✅ Explanation:
A function func is defined.
It contains try, except, and finally blocks.
๐น 2. try Block
try:
print("A")
return 1
✅ Explanation:
First, "A" is printed.
Then return 1 is executed.
⚠️ Important:
Even though return is reached, Python does NOT immediately exit
It will still execute the finally block
๐น 3. except Block
except:
print("B")
✅ Explanation:
Runs only if an exception occurs in try
In this case:
No error happens
So this block is skipped
๐น 4. finally Block
finally:
print("C")
✅ Explanation:
This block always executes, no matter what:
Return
Exception
Normal execution
๐ So "C" is printed even after return
๐น 5. Calling the Function
print(func())
๐ Step-by-step execution:
print("A") → prints:
A
return 1 is prepared (but paused)
finally runs → prints:
C
Function returns 1
Outer print() prints:
1
๐ฏ Final Output
A
C
1

0 Comments:
Post a Comment