Python's try finally (it's not easy)

Source: Internet
Author: User

Https://www.cnblogs.com/cotton/p/3785999.html

def f ():     Try :         Print 1        return 1    finally:        print  0         return 0

Print F ()
The result is 1 0 0
With the result of the operation, it is indicated that no matter what is executed in the try, even the return will call finally .

def f ():     Try :         Print 1        return 1    finally:        print  0         #return 0

To shield the finally return, you may have guessed the result.

The result is 1 0 1

Are you surprised?

Comparing the previous result, the return value of the try is overwritten by the return value of finally , perhaps because a function can have only one return value, whichever is the last result

def f ():     Try :         Print 1        return 1    except:        return 2    Else  :        print 3        return 3    finally:         Print 0         # return 0

Do you think that no exception else will execute?

As you expected, and did not execute.

The result is 1 0 1

Indicates that return after the tryhas been run, blocking else execution , but does not affect the execution of the finally.

Borrow two words from Vamer article:

" If there is no exception in the try, then the except section skips and executes the statement in else ." (Provided that there is no return value in the try)

Finally, there are some things to do, whether or not there is an exception. "(regardless of whether there is a return value in the try)

It adds that, in the case of a return, it does not hinder the execution of the finally . (but will hinder else)

Added a bit of testing:

#根据 https://www.cnblogs.com/cotton/p/3785999.html "Some questions about try...finally in Python"def test_finally_return1 (): Try:print (1) return1Finally:print (0) return0def test_finally_return2 (): Try:print (1) return1Finally:print (0) #return0def test_else_finally1 (): Try:print (1) return1Except:return2    Else: Print (3) return3Finally:print (0) #return0def test_else_finally2 (): Try:print (1) return1Except:return2    Else: Print (3) #return3Finally:print (0) return0def test_else_finally3 (): Try:print (1) #return1Except:print (2) #return2    Else: Print (3) #return3Finally:print (0) return0def test_else_return1 (): Try:print (1) return1Except:return2    Else: Print (3) return3# finally: # print (0) #return0def test_else_return2 (): Try:print (1) #return1Except:return2    Else: Print (3) return3# finally: # print (0) #return0if__name__ = ='__main__': Print ('Test 1') Print (TEST_FINALLY_RETURN1 ()) Print ('Test 2') Print (test_finally_return2 ()) Print ('Test 3') Print (Test_else_finally1 ()) Print ('Test 4') Print (Test_else_finally2 ()) Print ('Test 5') Print (TEST_ELSE_RETURN1 ()) Print ('Test 6') Print (test_else_return2 ()) Print ('Test 7') Print (Test_else_finally3 ())

Test results:

? Test Python3 test_try_finally.py Testing 1100Test 2101Test 3101Test 4100Test 511Test 6133Test 71300

Self-Summary:

Do not write the return value in try else. If there is no finally, it is written in the last, or only in the finally.

Try except else is doing something instead of handling the return

Python's try finally (it's not easy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.