finally blocks

This commit is contained in:
rasbt 2014-04-25 08:03:47 -04:00
parent 0bef7c1add
commit 381c1d1e97
2 changed files with 230 additions and 22 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:1f7175fddf00a2283246009f1da253d4c1f87bc6ac35f6c2eea1b216e927254e"
"signature": "sha256:df40791be1e08052b49de55275b6025defd84acad805842af17f14781fca9db3"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -78,7 +78,8 @@
"- [Reusing global variable names and UnboundLocalErrors](#unboundlocalerror)\n",
"- [Creating copies of mutable objects](#copy_mutable)\n",
"- [Key differences between Python 2 and 3](#python_differences)\n",
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)"
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
"- [Abortive statements in `finally` blocks](#finally_blocks)"
]
},
{
@ -336,6 +337,13 @@
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -350,13 +358,6 @@
"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):"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "code",
"collapsed": false,
@ -492,14 +493,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"List modification of the original list does affect shallow copies, but not deep copies if the list contains compound objects."
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
"List modification of the original list does affect shallow copies, but not deep copies if the list contains compound objects."
]
},
{
@ -2675,6 +2676,109 @@
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"<a name=\"finally_blocks\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Abortive statements in `finally` blocks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def try_finally1():\n",
" try:\n",
" print('in try:')\n",
" print('do some stuff')\n",
" float('abc')\n",
" except ValueError:\n",
" print('an error occurred')\n",
" else:\n",
" print('no error occurred')\n",
" finally:\n",
" print('always execute finally')\n",
" \n",
"try_finally1()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"in try:\n",
"do some stuff\n",
"an error occurred\n",
"always execute finally\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"But can you also guess what will be printed in the next code cell?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def try_finally2():\n",
" try:\n",
" print(\"do some stuff in try block\")\n",
" return \"return from try block\"\n",
" finally:\n",
" print(\"do some stuff in finally block\")\n",
" return \"always execute finally\"\n",
" \n",
"print(try_finally2())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"do some stuff in try block\n",
"do some stuff in finally block\n",
"always execute finally\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"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!"
]
},
{
"cell_type": "code",
"collapsed": false,

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:1f7175fddf00a2283246009f1da253d4c1f87bc6ac35f6c2eea1b216e927254e"
"signature": "sha256:df40791be1e08052b49de55275b6025defd84acad805842af17f14781fca9db3"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -78,7 +78,8 @@
"- [Reusing global variable names and UnboundLocalErrors](#unboundlocalerror)\n",
"- [Creating copies of mutable objects](#copy_mutable)\n",
"- [Key differences between Python 2 and 3](#python_differences)\n",
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)"
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
"- [Abortive statements in `finally` blocks](#finally_blocks)"
]
},
{
@ -336,6 +337,13 @@
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -350,13 +358,6 @@
"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):"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "code",
"collapsed": false,
@ -492,14 +493,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"List modification of the original list does affect shallow copies, but not deep copies if the list contains compound objects."
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
"List modification of the original list does affect shallow copies, but not deep copies if the list contains compound objects."
]
},
{
@ -2675,6 +2676,109 @@
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"<a name=\"finally_blocks\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Abortive statements in `finally` blocks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def try_finally1():\n",
" try:\n",
" print('in try:')\n",
" print('do some stuff')\n",
" float('abc')\n",
" except ValueError:\n",
" print('an error occurred')\n",
" else:\n",
" print('no error occurred')\n",
" finally:\n",
" print('always execute finally')\n",
" \n",
"try_finally1()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"in try:\n",
"do some stuff\n",
"an error occurred\n",
"always execute finally\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"But can you also guess what will be printed in the next code cell?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def try_finally2():\n",
" try:\n",
" print(\"do some stuff in try block\")\n",
" return \"return from try block\"\n",
" finally:\n",
" print(\"do some stuff in finally block\")\n",
" return \"always execute finally\"\n",
" \n",
"print(try_finally2())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"do some stuff in try block\n",
"do some stuff in finally block\n",
"always execute finally\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"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!"
]
},
{
"cell_type": "code",
"collapsed": false,