|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:29a120258e2d108ed5eace08e071ad866ae379b4f24fde804401ee858a2090fb" |
| 4 | + "signature": "sha256:39951e0d86a78e2a0068b01db48a79c51e896c8f05c69e387c77e0fb68eaec41" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
404 | 404 | "\n", |
405 | 405 | "If both values of in a `or` expression are True, Python will select the first one, and the second one in `and` expressions\n", |
406 | 406 | "\n", |
| 407 | + "Or - as a reader suggested - picture it as \n", |
| 408 | + "`a or b == a if a else b` \n", |
| 409 | + "`a and b == b if a else a` \n", |
| 410 | + "\n", |
407 | 411 | "(Original source: [http://gistroll.com/rolls/21/horizontal_assessments/new](http://gistroll.com/rolls/21/horizontal_assessments/new))" |
408 | 412 | ] |
409 | 413 | }, |
|
427 | 431 | ], |
428 | 432 | "prompt_number": 9 |
429 | 433 | }, |
430 | | - { |
431 | | - "cell_type": "markdown", |
432 | | - "metadata": {}, |
433 | | - "source": [ |
434 | | - "And a fun fact" |
435 | | - ] |
436 | | - }, |
437 | 434 | { |
438 | 435 | "cell_type": "markdown", |
439 | 436 | "metadata": {}, |
|
448 | 445 | "cell_type": "markdown", |
449 | 446 | "metadata": {}, |
450 | 447 | "source": [ |
451 | | - "Don't use mutable objects (e.g., dictionaries, lists, sets, etc.) as default arguments for functions! You might expect that a new list is created every time when we call the function without providing an argument for the default parameter, but this is not the case: Python will create the mutable object (default parameter) only the first time the function is called, see the following code:\n", |
| 448 | + "Don't use mutable objects (e.g., dictionaries, lists, sets, etc.) as default arguments for functions! You might expect that a new list is created every time when we call the function without providing an argument for the default parameter, but this is not the case: Python will create the mutable object (default parameter) the first time the function is defined - not when it is called, see the following code:\n", |
452 | 449 | "\n", |
453 | 450 | "(Original source: [http://docs.python-guide.org/en/latest/writing/gotchas/](http://docs.python-guide.org/en/latest/writing/gotchas/)" |
454 | 451 | ] |
|
599 | 596 | "\n", |
600 | 597 | "## About lambda and closures-in-a-loop pitfall\n", |
601 | 598 | "\n", |
602 | | - "The following example illustrates how the (last) `lambda` is being reused:\n", |
| 599 | + "Remember the [\"consuming generators\"](consuming_generators)? This example is somewhat related, but the result might still come unexpected. \n", |
603 | 600 | "\n", |
604 | | - "(Original source: [http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html](http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html))" |
| 601 | + "(Original source: [http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html](http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html))\n", |
| 602 | + "\n", |
| 603 | + "In the first example below, where we call a `lambda` function in a list comprehension, the value `i` is dereferenced every time we call `lambda` within the scope of the list comprehension. Since the list is already constructed when we `for-loop` through the list, it is set to the last value 4." |
605 | 604 | ] |
606 | 605 | }, |
607 | 606 | { |
|
627 | 626 | ] |
628 | 627 | } |
629 | 628 | ], |
630 | | - "prompt_number": 24 |
| 629 | + "prompt_number": 11 |
631 | 630 | }, |
632 | 631 | { |
633 | 632 | "cell_type": "markdown", |
634 | 633 | "metadata": {}, |
635 | 634 | "source": [ |
636 | | - "**Here, a generator can save you some pain:**" |
| 635 | + "This, however, does not apply to generators:" |
637 | 636 | ] |
638 | 637 | }, |
639 | 638 | { |
|
659 | 658 | ] |
660 | 659 | } |
661 | 660 | ], |
662 | | - "prompt_number": 25 |
| 661 | + "prompt_number": 9 |
663 | 662 | }, |
664 | 663 | { |
665 | 664 | "cell_type": "markdown", |
|
985 | 984 | "\n", |
986 | 985 | "**A. Jesse Jiryu Davis** has a nice explanation for this phenomenon (Original source: [http://emptysqua.re/blog/python-increment-is-weird-part-ii/](http://emptysqua.re/blog/python-increment-is-weird-part-ii/))\n", |
987 | 986 | "\n", |
988 | | - "If we try to extend the list via `+=` *\"then the statement executes STORE_SUBSCR, which calls the C function PyObject_SetItem, which checks if the object supports item assignment. In our case the object is a tuple, so PyObject_SetItem throws the TypeError. Mystery solved.\"*" |
| 987 | + "If we try to extend the list via `+=` *\"then the statement executes `STORE_SUBSCR`, which calls the C function `PyObject_SetItem`, which checks if the object supports item assignment. In our case the object is a tuple, so `PyObject_SetItem` throws the `TypeError`. Mystery solved.\"*" |
989 | 988 | ] |
990 | 989 | }, |
991 | 990 | { |
|
0 commit comments