|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:1f7175fddf00a2283246009f1da253d4c1f87bc6ac35f6c2eea1b216e927254e" |
| 4 | + "signature": "sha256:df40791be1e08052b49de55275b6025defd84acad805842af17f14781fca9db3" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
78 | 78 | "- [Reusing global variable names and UnboundLocalErrors](#unboundlocalerror)\n", |
79 | 79 | "- [Creating copies of mutable objects](#copy_mutable)\n", |
80 | 80 | "- [Key differences between Python 2 and 3](#python_differences)\n", |
81 | | - "- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)" |
| 81 | + "- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n", |
| 82 | + "- [Abortive statements in `finally` blocks](#finally_blocks)" |
82 | 83 | ] |
83 | 84 | }, |
84 | 85 | { |
|
336 | 337 | "\n" |
337 | 338 | ] |
338 | 339 | }, |
| 340 | + { |
| 341 | + "cell_type": "markdown", |
| 342 | + "metadata": {}, |
| 343 | + "source": [ |
| 344 | + "[[back to top](#sections)]" |
| 345 | + ] |
| 346 | + }, |
339 | 347 | { |
340 | 348 | "cell_type": "markdown", |
341 | 349 | "metadata": {}, |
|
350 | 358 | "Here is a [nice article](http://python.net/%7Egoodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) explaining it using \"boxes\" (for people with C background) and \"name tags\" (in the case of Python):" |
351 | 359 | ] |
352 | 360 | }, |
353 | | - { |
354 | | - "cell_type": "markdown", |
355 | | - "metadata": {}, |
356 | | - "source": [ |
357 | | - "[[back to top](#sections)]" |
358 | | - ] |
359 | | - }, |
360 | 361 | { |
361 | 362 | "cell_type": "code", |
362 | 363 | "collapsed": false, |
|
492 | 493 | "cell_type": "markdown", |
493 | 494 | "metadata": {}, |
494 | 495 | "source": [ |
495 | | - "List modification of the original list does affect shallow copies, but not deep copies if the list contains compound objects." |
| 496 | + "[[back to top](#sections)]" |
496 | 497 | ] |
497 | 498 | }, |
498 | 499 | { |
499 | 500 | "cell_type": "markdown", |
500 | 501 | "metadata": {}, |
501 | 502 | "source": [ |
502 | | - "[[back to top](#sections)]" |
| 503 | + "List modification of the original list does affect shallow copies, but not deep copies if the list contains compound objects." |
503 | 504 | ] |
504 | 505 | }, |
505 | 506 | { |
|
2675 | 2676 | "..." |
2676 | 2677 | ] |
2677 | 2678 | }, |
| 2679 | + { |
| 2680 | + "cell_type": "markdown", |
| 2681 | + "metadata": {}, |
| 2682 | + "source": [ |
| 2683 | + "<br>\n", |
| 2684 | + "<br>\n", |
| 2685 | + "<a name=\"finally_blocks\"></a>" |
| 2686 | + ] |
| 2687 | + }, |
| 2688 | + { |
| 2689 | + "cell_type": "markdown", |
| 2690 | + "metadata": {}, |
| 2691 | + "source": [ |
| 2692 | + "# Abortive statements in `finally` blocks" |
| 2693 | + ] |
| 2694 | + }, |
| 2695 | + { |
| 2696 | + "cell_type": "markdown", |
| 2697 | + "metadata": {}, |
| 2698 | + "source": [ |
| 2699 | + "Python's `try-except-finally` blocks are very handy for catching and handling errors. The `finally` block is always executed whether an `exception` has been raised or not as illustrated in the following example." |
| 2700 | + ] |
| 2701 | + }, |
| 2702 | + { |
| 2703 | + "cell_type": "code", |
| 2704 | + "collapsed": false, |
| 2705 | + "input": [ |
| 2706 | + "def try_finally1():\n", |
| 2707 | + " try:\n", |
| 2708 | + " print('in try:')\n", |
| 2709 | + " print('do some stuff')\n", |
| 2710 | + " float('abc')\n", |
| 2711 | + " except ValueError:\n", |
| 2712 | + " print('an error occurred')\n", |
| 2713 | + " else:\n", |
| 2714 | + " print('no error occurred')\n", |
| 2715 | + " finally:\n", |
| 2716 | + " print('always execute finally')\n", |
| 2717 | + " \n", |
| 2718 | + "try_finally1()" |
| 2719 | + ], |
| 2720 | + "language": "python", |
| 2721 | + "metadata": {}, |
| 2722 | + "outputs": [ |
| 2723 | + { |
| 2724 | + "output_type": "stream", |
| 2725 | + "stream": "stdout", |
| 2726 | + "text": [ |
| 2727 | + "in try:\n", |
| 2728 | + "do some stuff\n", |
| 2729 | + "an error occurred\n", |
| 2730 | + "always execute finally\n" |
| 2731 | + ] |
| 2732 | + } |
| 2733 | + ], |
| 2734 | + "prompt_number": 24 |
| 2735 | + }, |
| 2736 | + { |
| 2737 | + "cell_type": "markdown", |
| 2738 | + "metadata": {}, |
| 2739 | + "source": [ |
| 2740 | + "<br>\n", |
| 2741 | + "<br>\n", |
| 2742 | + "But can you also guess what will be printed in the next code cell?" |
| 2743 | + ] |
| 2744 | + }, |
| 2745 | + { |
| 2746 | + "cell_type": "code", |
| 2747 | + "collapsed": false, |
| 2748 | + "input": [ |
| 2749 | + "def try_finally2():\n", |
| 2750 | + " try:\n", |
| 2751 | + " print(\"do some stuff in try block\")\n", |
| 2752 | + " return \"return from try block\"\n", |
| 2753 | + " finally:\n", |
| 2754 | + " print(\"do some stuff in finally block\")\n", |
| 2755 | + " return \"always execute finally\"\n", |
| 2756 | + " \n", |
| 2757 | + "print(try_finally2())" |
| 2758 | + ], |
| 2759 | + "language": "python", |
| 2760 | + "metadata": {}, |
| 2761 | + "outputs": [ |
| 2762 | + { |
| 2763 | + "output_type": "stream", |
| 2764 | + "stream": "stdout", |
| 2765 | + "text": [ |
| 2766 | + "do some stuff in try block\n", |
| 2767 | + "do some stuff in finally block\n", |
| 2768 | + "always execute finally\n" |
| 2769 | + ] |
| 2770 | + } |
| 2771 | + ], |
| 2772 | + "prompt_number": 21 |
| 2773 | + }, |
| 2774 | + { |
| 2775 | + "cell_type": "markdown", |
| 2776 | + "metadata": {}, |
| 2777 | + "source": [ |
| 2778 | + "<br>\n", |
| 2779 | + "Here, the abortive `return` statement in the `finally` block simply overrules the `return` in the `try` block, since **`finally` is guaranteed to always be executed.** So, be careful using abortive statements in `finally` blocks!" |
| 2780 | + ] |
| 2781 | + }, |
2678 | 2782 | { |
2679 | 2783 | "cell_type": "code", |
2680 | 2784 | "collapsed": false, |
|
0 commit comments