Skip to content

Commit ac689da

Browse files
committed
lambda update
1 parent 69b2774 commit ac689da

2 files changed

Lines changed: 27 additions & 29 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:29a120258e2d108ed5eace08e071ad866ae379b4f24fde804401ee858a2090fb"
4+
"signature": "sha256:39951e0d86a78e2a0068b01db48a79c51e896c8f05c69e387c77e0fb68eaec41"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -404,6 +404,10 @@
404404
"\n",
405405
"If both values of in a `or` expression are True, Python will select the first one, and the second one in `and` expressions\n",
406406
"\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",
407411
"(Original source: [http://gistroll.com/rolls/21/horizontal_assessments/new](http://gistroll.com/rolls/21/horizontal_assessments/new))"
408412
]
409413
},
@@ -427,13 +431,6 @@
427431
],
428432
"prompt_number": 9
429433
},
430-
{
431-
"cell_type": "markdown",
432-
"metadata": {},
433-
"source": [
434-
"And a fun fact"
435-
]
436-
},
437434
{
438435
"cell_type": "markdown",
439436
"metadata": {},
@@ -448,7 +445,7 @@
448445
"cell_type": "markdown",
449446
"metadata": {},
450447
"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",
452449
"\n",
453450
"(Original source: [http://docs.python-guide.org/en/latest/writing/gotchas/](http://docs.python-guide.org/en/latest/writing/gotchas/)"
454451
]
@@ -599,9 +596,11 @@
599596
"\n",
600597
"## About lambda and closures-in-a-loop pitfall\n",
601598
"\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",
603600
"\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."
605604
]
606605
},
607606
{
@@ -627,13 +626,13 @@
627626
]
628627
}
629628
],
630-
"prompt_number": 24
629+
"prompt_number": 11
631630
},
632631
{
633632
"cell_type": "markdown",
634633
"metadata": {},
635634
"source": [
636-
"**Here, a generator can save you some pain:**"
635+
"This, however, does not apply to generators:"
637636
]
638637
},
639638
{
@@ -659,7 +658,7 @@
659658
]
660659
}
661660
],
662-
"prompt_number": 25
661+
"prompt_number": 9
663662
},
664663
{
665664
"cell_type": "markdown",
@@ -985,7 +984,7 @@
985984
"\n",
986985
"**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",
987986
"\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.\"*"
989988
]
990989
},
991990
{

not_so_obvious_python_stuff.ipynb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:5f8052b5458fbe30e4066fd51688157e823dbbcce9f2e4ef402acfc6af40d046"
4+
"signature": "sha256:39951e0d86a78e2a0068b01db48a79c51e896c8f05c69e387c77e0fb68eaec41"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -404,6 +404,10 @@
404404
"\n",
405405
"If both values of in a `or` expression are True, Python will select the first one, and the second one in `and` expressions\n",
406406
"\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",
407411
"(Original source: [http://gistroll.com/rolls/21/horizontal_assessments/new](http://gistroll.com/rolls/21/horizontal_assessments/new))"
408412
]
409413
},
@@ -427,13 +431,6 @@
427431
],
428432
"prompt_number": 9
429433
},
430-
{
431-
"cell_type": "markdown",
432-
"metadata": {},
433-
"source": [
434-
"And a fun fact"
435-
]
436-
},
437434
{
438435
"cell_type": "markdown",
439436
"metadata": {},
@@ -448,7 +445,7 @@
448445
"cell_type": "markdown",
449446
"metadata": {},
450447
"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",
452449
"\n",
453450
"(Original source: [http://docs.python-guide.org/en/latest/writing/gotchas/](http://docs.python-guide.org/en/latest/writing/gotchas/)"
454451
]
@@ -599,9 +596,11 @@
599596
"\n",
600597
"## About lambda and closures-in-a-loop pitfall\n",
601598
"\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",
603600
"\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."
605604
]
606605
},
607606
{
@@ -627,13 +626,13 @@
627626
]
628627
}
629628
],
630-
"prompt_number": 24
629+
"prompt_number": 11
631630
},
632631
{
633632
"cell_type": "markdown",
634633
"metadata": {},
635634
"source": [
636-
"**Here, a generator can save you some pain:**"
635+
"This, however, does not apply to generators:"
637636
]
638637
},
639638
{
@@ -659,7 +658,7 @@
659658
]
660659
}
661660
],
662-
"prompt_number": 25
661+
"prompt_number": 9
663662
},
664663
{
665664
"cell_type": "markdown",

0 commit comments

Comments
 (0)