Skip to content

Commit 38dd7c7

Browse files
Add a few more teaching points on error strategies
1 parent 6d95ad2 commit 38dd7c7

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
.DS_Store
66
.ipynb_checkpoints
77
tmp-file.txt
8+
__pycache__

bootstrap.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Bootstrap
2+
=========
3+
4+
If you want to follow along during this tutorial then
5+
I recommend the Anaconda Python3 distribution:
6+
7+
https://www.anaconda.com/download/
8+
9+
For a quick start on CERN resources try:
10+
11+
12+
$ source /cvmfs/sft.cern.ch/lcg/views/LCG_93python3/x86_64-centos7-gcc7-opt/setup.sh
13+
$ ipython
14+
Python 3.6.3 (default, Dec 18 2017, 11:58:54)
15+
Type "copyright", "credits" or "license" for more information.
16+
17+
IPython 5.4.1 -- An enhanced Interactive Python.
18+
? -> Introduction and overview of IPython's features.
19+
%quickref -> Quick reference.
20+
help -> Python's own help system.
21+
object? -> Details about 'object', use 'object??' for extra details.
22+
23+
In [1]:

presentation-insights.ipynb

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,7 +3344,11 @@
33443344
{
33453345
"cell_type": "code",
33463346
"execution_count": 161,
3347-
"metadata": {},
3347+
"metadata": {
3348+
"slideshow": {
3349+
"slide_type": "-"
3350+
}
3351+
},
33483352
"outputs": [
33493353
{
33503354
"name": "stdout",
@@ -3362,7 +3366,11 @@
33623366
{
33633367
"cell_type": "code",
33643368
"execution_count": 162,
3365-
"metadata": {},
3369+
"metadata": {
3370+
"slideshow": {
3371+
"slide_type": "fragment"
3372+
}
3373+
},
33663374
"outputs": [
33673375
{
33683376
"name": "stdout",
@@ -3401,7 +3409,7 @@
34013409
"source": [
34023410
"This is pretty easy - any python file found in the current directory can be imported as a module, then it becomes available, using the filename as the namespace entry\n",
34033411
"\n",
3404-
"Actually, the files don't need to live in the currect directory, `$PYTHONPATH` gets searched , or `sys.path` "
3412+
"Actually, the files don't need to live in the currect directory, `$PYTHONPATH` gets searched (from the shell), or `sys.path` inside Python itself"
34053413
]
34063414
},
34073415
{
@@ -3496,6 +3504,7 @@
34963504
"* Class methods are defined very like functions, using `def`\n",
34973505
" * The first parameter is the class instance itself, by convention always called `self`\n",
34983506
"* The special method `__init__` is called when an instance of the class is created (a.k.a. a constructor)\n",
3507+
" * (BTW, there are lots of these special `__FOO__` attributes in Python, e.g., `__del__` is your destructor)\n",
34993508
"* All data members of the class are referenced via the object instance, `self`\n",
35003509
" * `self.counter` is a *data member* of the class\n",
35013510
" * `counter` would be a plain local variable (watch out!)"
@@ -3778,7 +3787,7 @@
37783787
"\n",
37793788
"On point that might be coming clear to you now is that Python is quite happy to pass *any* objects into function calls of methods\n",
37803789
"\n",
3781-
"If the passed object has the right properties to work with the call, it works; if not, then something will fail\n",
3790+
"If the passed object has the right properties to work with the call, it works; if not, then something will fail (this is known in the trade as *Duck Typing*)\n",
37823791
"\n",
37833792
"Two useful functions can be used to inspect a class's providence\n",
37843793
"* `isinstance(obj, classinfo)` returns `True` if the object is an instance of, or derived from, the classinfo class\n",
@@ -4336,6 +4345,52 @@
43364345
" pass # pass is a very handy bit of python syntax used for supporting an empty code block"
43374346
]
43384347
},
4348+
{
4349+
"cell_type": "markdown",
4350+
"metadata": {
4351+
"slideshow": {
4352+
"slide_type": "subslide"
4353+
}
4354+
},
4355+
"source": [
4356+
"## LBYL and EAFP\n",
4357+
"\n",
4358+
"Programming life could be divided into two strategies for dealing with errors\n",
4359+
"\n",
4360+
"* *Look Before You Leap* - check that things are going to be ok first\n",
4361+
"* *Easier to Ask Forgiveness than Permission* - go for it and clean up if you need to"
4362+
]
4363+
},
4364+
{
4365+
"cell_type": "markdown",
4366+
"metadata": {
4367+
"slideshow": {
4368+
"slide_type": "fragment"
4369+
}
4370+
},
4371+
"source": [
4372+
"```py\n",
4373+
"try:\n",
4374+
" x = my_dict[\"key\"]\n",
4375+
"except KeyError:\n",
4376+
" # handle missing key\n",
4377+
"\n",
4378+
"if \"key\" in my_dict:\n",
4379+
" x = my_dict[\"key\"]\n",
4380+
"else:\n",
4381+
" # handle missing key\n",
4382+
"```"
4383+
]
4384+
},
4385+
{
4386+
"cell_type": "markdown",
4387+
"metadata": {},
4388+
"source": [
4389+
"In general Python prefers EAFP - there are a few advantaged (like avoiding some race conditions) and generally the code looks rather cleaner\n",
4390+
"\n",
4391+
"However, don't get so carried away that you start to use exceptions as control flow (really, keep them for *exceptional* situations)"
4392+
]
4393+
},
43394394
{
43404395
"cell_type": "markdown",
43414396
"metadata": {

0 commit comments

Comments
 (0)