Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finally always runs just before the return in try block, then why update in finally block not affect value of variable returned by try block?

Tags:

Finally block runs just before the return statement in the try block, as shown in the below example - returns False instead of True:

>>> def bool_return(): ...  try: ...    return True ...  finally: ...    return False ... >>> bool_return() False 

Similarly, the following code returns value set in the Finally block:

>>> def num_return(): ...  try: ...    x=100 ...    return x ...  finally: ...    x=90 ...    return x ... >>> num_return() 90 

However, for variable assignment without return statement in the finally block, why does value of variable updated by the finally block not get returned by the try block? Is the variable from finally block scoped locally in the finally block? Or is the return value from the try block held in memory buffer and unaffected by assignment in finally block? In the below example, why is the output 100 instead of 90?

>>> def num_return(): ...  try: ...    x=100 ...    return x ...  finally: ...    x=90 ... >>> num_return() 100 

Similarly the following example:

In [1]: def num_return():    ...:   try:    ...:     x=[100]    ...:     return x    ...:   finally:    ...:     x[0] = 90    ...:  In [2]: num_return() Out[2]: [90]  In [3]: def num_return():    ...:   try:    ...:     x=[100]    ...:     return x[0]    ...:   finally:    ...:     x[0] = 90    ...:  In [4]: num_return() Out[4]: 100 
like image 736
variable Avatar asked Jul 01 '20 14:07

variable


People also ask

Does finally run before or after return?

finally trumps return. This is because when return i; is executed i has a value 2. After this the finally block is executed where 12 is assigned to i and then System.

Is finally block always executed after return?

Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.

Why finally block is always executed?

A finally block is always get executed whether the exception has occurred or not. If an exception occurs like closing a file or DB connection, then the finally block is used to clean up the code. We cannot say the finally block is always executes because sometimes if any statement like System.

Does finally run after try?

What Is finally? finally defines a block of code we use along with the try keyword. It defines code that's always run after the try and any catch block, before the method is completed. The finally block executes regardless of whether an exception is thrown or caught.


2 Answers

A little experiment to help confirm what others have answered, is to replace x with a single-value list, like this:

def num_return():   try:     x=[100]     return x   finally:     x[0] = 90 

now the value returned is [90], so the list is indeed modified in the finally block.

BUT if you return x[0], you get 100 (even though we just based the fact that the list itself does change in the finally block).

 In [1]: def num_return():    ...:   try:    ...:     x=[100]    ...:     return x    ...:   finally:    ...:     x[0] = 90    ...:  In [2]: num_return() Out[2]: [90]  In [3]: def num_return():    ...:   try:    ...:     x=[100]    ...:     return x[0]    ...:   finally:    ...:     x[0] = 90    ...:  In [4]: num_return() Out[4]: 100 
like image 84
Adam.Er8 Avatar answered Oct 20 '22 05:10

Adam.Er8


When you say return x, Python saves the value of the variable x at that point as the return value. Updating x later doesn't affect this.

like image 22
Mustafa Quraish Avatar answered Oct 20 '22 06:10

Mustafa Quraish