Commit b74668c3 authored by Loic Huder's avatar Loic Huder
Browse files

Added a small paragraph on EAFP at the end of pres02

parent 61b8b54c
Pipeline #32979 passed with stage
in 53 seconds
......@@ -144,9 +144,7 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"a = 4\n",
......@@ -226,9 +224,7 @@
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"b = bool('1')\n",
......@@ -430,9 +426,7 @@
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# float\n",
......@@ -584,7 +578,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n"
"['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n"
]
}
],
......@@ -1126,7 +1120,6 @@
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "-"
}
......@@ -1695,9 +1688,7 @@
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"s = 'wind;temperature;pressure'"
......@@ -2296,8 +2287,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"integer remove from the list: 9\n",
"shuffled list: [13, 16, 15, 14, 5, 17, 12, 8, 11, 1, 18, 2, 10, 19, 4, 7, 0, 6, 3]\n"
"integer remove from the list: 19\n",
"shuffled list: [9, 17, 15, 14, 1, 3, 18, 7, 6, 16, 4, 0, 12, 5, 13, 2, 11, 8, 10]\n"
]
}
],
......@@ -2343,9 +2334,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[13, 16, 15, 14, 5, 17, 12, 8, 11, 1, 18, 2, 10, 19, 4, 7, 0, 6, 3]\n",
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
"missing 9\n"
"[9, 17, 15, 14, 1, 3, 18, 7, 6, 16, 4, 0, 12, 5, 13, 2, 11, 8, 10]\n",
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]\n",
"missing 18\n"
]
}
],
......@@ -2374,7 +2365,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"missing 9\n"
"missing 19\n"
]
}
],
......@@ -2395,7 +2386,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"missing 9\n"
"missing 19\n"
]
}
],
......@@ -2604,10 +2595,36 @@
" \n",
"print(split_list)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Bonus: the Easier to Ask for Forgiveness than Permission (EAFP) principle\n",
"\n",
"There are several ways to perform the previous task:\n",
"\n",
"- In the solution presented above, we `try` first the conversion of the string in number and ask for *forgiveness* if we encounter an error (by using the `except`).\n",
"- Or we can test if the string is a number, using for example `str.isdigit()` or `str.isdecimal()` (*asking for permission*) and do the conversion only in this case.\n",
"\n",
"Both approches will lead to the same result so it is rather a question of principle. In Python, it is generally prefered to use `try` and `except` to follow the EAFP principle.\n",
"In this particular case, using `try` and `except` is far easier !"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Slideshow",
"celltoolbar": "Diaporama",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
......@@ -2623,18 +2640,18 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.6.8"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false
}
},
"source": []
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment