Skip to content

Commit 381c1d1

Browse files
committed
finally blocks
1 parent 0bef7c1 commit 381c1d1

2 files changed

Lines changed: 230 additions & 22 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:1f7175fddf00a2283246009f1da253d4c1f87bc6ac35f6c2eea1b216e927254e"
4+
"signature": "sha256:df40791be1e08052b49de55275b6025defd84acad805842af17f14781fca9db3"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -78,7 +78,8 @@
7878
"- [Reusing global variable names and UnboundLocalErrors](#unboundlocalerror)\n",
7979
"- [Creating copies of mutable objects](#copy_mutable)\n",
8080
"- [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)"
8283
]
8384
},
8485
{
@@ -336,6 +337,13 @@
336337
"\n"
337338
]
338339
},
340+
{
341+
"cell_type": "markdown",
342+
"metadata": {},
343+
"source": [
344+
"[[back to top](#sections)]"
345+
]
346+
},
339347
{
340348
"cell_type": "markdown",
341349
"metadata": {},
@@ -350,13 +358,6 @@
350358
"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):"
351359
]
352360
},
353-
{
354-
"cell_type": "markdown",
355-
"metadata": {},
356-
"source": [
357-
"[[back to top](#sections)]"
358-
]
359-
},
360361
{
361362
"cell_type": "code",
362363
"collapsed": false,
@@ -492,14 +493,14 @@
492493
"cell_type": "markdown",
493494
"metadata": {},
494495
"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)]"
496497
]
497498
},
498499
{
499500
"cell_type": "markdown",
500501
"metadata": {},
501502
"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."
503504
]
504505
},
505506
{
@@ -2675,6 +2676,109 @@
26752676
"..."
26762677
]
26772678
},
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+
},
26782782
{
26792783
"cell_type": "code",
26802784
"collapsed": false,

not_so_obvious_python_stuff.ipynb

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:1f7175fddf00a2283246009f1da253d4c1f87bc6ac35f6c2eea1b216e927254e"
4+
"signature": "sha256:df40791be1e08052b49de55275b6025defd84acad805842af17f14781fca9db3"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -78,7 +78,8 @@
7878
"- [Reusing global variable names and UnboundLocalErrors](#unboundlocalerror)\n",
7979
"- [Creating copies of mutable objects](#copy_mutable)\n",
8080
"- [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)"
8283
]
8384
},
8485
{
@@ -336,6 +337,13 @@
336337
"\n"
337338
]
338339
},
340+
{
341+
"cell_type": "markdown",
342+
"metadata": {},
343+
"source": [
344+
"[[back to top](#sections)]"
345+
]
346+
},
339347
{
340348
"cell_type": "markdown",
341349
"metadata": {},
@@ -350,13 +358,6 @@
350358
"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):"
351359
]
352360
},
353-
{
354-
"cell_type": "markdown",
355-
"metadata": {},
356-
"source": [
357-
"[[back to top](#sections)]"
358-
]
359-
},
360361
{
361362
"cell_type": "code",
362363
"collapsed": false,
@@ -492,14 +493,14 @@
492493
"cell_type": "markdown",
493494
"metadata": {},
494495
"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)]"
496497
]
497498
},
498499
{
499500
"cell_type": "markdown",
500501
"metadata": {},
501502
"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."
503504
]
504505
},
505506
{
@@ -2675,6 +2676,109 @@
26752676
"..."
26762677
]
26772678
},
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+
},
26782782
{
26792783
"cell_type": "code",
26802784
"collapsed": false,

0 commit comments

Comments
 (0)