|
3344 | 3344 | {
|
3345 | 3345 | "cell_type": "code",
|
3346 | 3346 | "execution_count": 161,
|
3347 |
| - "metadata": {}, |
| 3347 | + "metadata": { |
| 3348 | + "slideshow": { |
| 3349 | + "slide_type": "-" |
| 3350 | + } |
| 3351 | + }, |
3348 | 3352 | "outputs": [
|
3349 | 3353 | {
|
3350 | 3354 | "name": "stdout",
|
|
3362 | 3366 | {
|
3363 | 3367 | "cell_type": "code",
|
3364 | 3368 | "execution_count": 162,
|
3365 |
| - "metadata": {}, |
| 3369 | + "metadata": { |
| 3370 | + "slideshow": { |
| 3371 | + "slide_type": "fragment" |
| 3372 | + } |
| 3373 | + }, |
3366 | 3374 | "outputs": [
|
3367 | 3375 | {
|
3368 | 3376 | "name": "stdout",
|
|
3401 | 3409 | "source": [
|
3402 | 3410 | "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",
|
3403 | 3411 | "\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" |
3405 | 3413 | ]
|
3406 | 3414 | },
|
3407 | 3415 | {
|
|
3496 | 3504 | "* Class methods are defined very like functions, using `def`\n",
|
3497 | 3505 | " * The first parameter is the class instance itself, by convention always called `self`\n",
|
3498 | 3506 | "* 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", |
3499 | 3508 | "* All data members of the class are referenced via the object instance, `self`\n",
|
3500 | 3509 | " * `self.counter` is a *data member* of the class\n",
|
3501 | 3510 | " * `counter` would be a plain local variable (watch out!)"
|
|
3778 | 3787 | "\n",
|
3779 | 3788 | "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",
|
3780 | 3789 | "\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", |
3782 | 3791 | "\n",
|
3783 | 3792 | "Two useful functions can be used to inspect a class's providence\n",
|
3784 | 3793 | "* `isinstance(obj, classinfo)` returns `True` if the object is an instance of, or derived from, the classinfo class\n",
|
|
4336 | 4345 | " pass # pass is a very handy bit of python syntax used for supporting an empty code block"
|
4337 | 4346 | ]
|
4338 | 4347 | },
|
| 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 | + }, |
4339 | 4394 | {
|
4340 | 4395 | "cell_type": "markdown",
|
4341 | 4396 | "metadata": {
|
|
0 commit comments