Skip to content

Commit 31d4382

Browse files
authored
Add new snippet: Yielding None
Add new snippet: Yielding None
2 parents 74e857e + e2c2cf1 commit 31d4382

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

README.md

+38-4
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ So, here ya go...
8383
- [💡 Explanation:](#-explanation-18)
8484
- [Needle in a Haystack](#needle-in-a-haystack)
8585
- [💡 Explanation:](#-explanation-19)
86-
- [The surprising comma](#the-surprising-comma)
86+
- [yielding None](#yielding-none)
8787
- [💡 Explanation:](#-explanation-20)
88-
- [For what?](#for-what)
88+
- [The surprising comma](#the-surprising-comma)
8989
- [💡 Explanation:](#-explanation-21)
90-
- [not knot!](#not-knot)
90+
- [For what?](#for-what)
9191
- [💡 Explanation:](#-explanation-22)
92-
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
92+
- [not knot!](#not-knot)
9393
- [💡 Explanation:](#-explanation-23)
94+
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
95+
- [💡 Explanation:](#-explanation-24)
9496
- [Minor Ones](#minor-ones)
9597
- [TODO: Hell of an example!](#todo-hell-of-an-example)
9698
- [Contributing](#contributing)
@@ -1491,6 +1493,38 @@ tuple()
14911493
14921494
---
14931495
1496+
### yielding None
1497+
1498+
Suggested by @chris-rands in [this](https://github.com/satwikkansal/wtfpython/issues/32) issue.
1499+
1500+
```py
1501+
some_iterable = ('a', 'b')
1502+
1503+
def some_func(val):
1504+
return "something"
1505+
```
1506+
1507+
1508+
**Output:**
1509+
```py
1510+
>>> [x for x in some_iterable]
1511+
['a', 'b']
1512+
>>> [(yield x) for x in some_iterable]
1513+
<generator object <listcomp> at 0x7f70b0a4ad58>
1514+
>>> list([(yield x) for x in some_iterable])
1515+
['a', 'b']
1516+
>>> list((yield x) for x in some_iterable)
1517+
['a', None, 'b', None]
1518+
>>> list(some_func((yield x)) for x in some_iterable)
1519+
['a', 'something', 'b', 'something']
1520+
```
1521+
1522+
#### 💡 Explanation:
1523+
- Source and explanation can be found here: https://stackoverflow.com/questions/32139885/yield-in-list-comprehensions-and-generator-expressions
1524+
- Related bug report: http://bugs.python.org/issue10544
1525+
1526+
---
1527+
14941528
### The surprising comma
14951529
14961530
Suggested by @MostAwesomeDude in [this](https://github.com/satwikkansal/wtfPython/issues/1) issue.

0 commit comments

Comments
 (0)