Skip to content

Commit

Permalink
Add new snippet: Yielding None
Browse files Browse the repository at this point in the history
Add new snippet: Yielding None
  • Loading branch information
satwikkansal authored Oct 11, 2017
2 parents 74e857e + e2c2cf1 commit 31d4382
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ So, here ya go...
- [πŸ’‘ Explanation:](#-explanation-18)
- [Needle in a Haystack](#needle-in-a-haystack)
- [πŸ’‘ Explanation:](#-explanation-19)
- [The surprising comma](#the-surprising-comma)
- [yielding None](#yielding-none)
- [πŸ’‘ Explanation:](#-explanation-20)
- [For what?](#for-what)
- [The surprising comma](#the-surprising-comma)
- [πŸ’‘ Explanation:](#-explanation-21)
- [not knot!](#not-knot)
- [For what?](#for-what)
- [πŸ’‘ Explanation:](#-explanation-22)
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
- [not knot!](#not-knot)
- [πŸ’‘ Explanation:](#-explanation-23)
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
- [πŸ’‘ Explanation:](#-explanation-24)
- [Minor Ones](#minor-ones)
- [TODO: Hell of an example!](#todo-hell-of-an-example)
- [Contributing](#contributing)
Expand Down Expand Up @@ -1491,6 +1493,38 @@ tuple()

---

### yielding None

Suggested by @chris-rands in [this](https://github.com/satwikkansal/wtfpython/issues/32) issue.

```py
some_iterable = ('a', 'b')

def some_func(val):
return "something"
```


**Output:**
```py
>>> [x for x in some_iterable]
['a', 'b']
>>> [(yield x) for x in some_iterable]
<generator object <listcomp> at 0x7f70b0a4ad58>
>>> list([(yield x) for x in some_iterable])
['a', 'b']
>>> list((yield x) for x in some_iterable)
['a', None, 'b', None]
>>> list(some_func((yield x)) for x in some_iterable)
['a', 'something', 'b', 'something']
```

#### πŸ’‘ Explanation:
- Source and explanation can be found here: https://stackoverflow.com/questions/32139885/yield-in-list-comprehensions-and-generator-expressions
- Related bug report: http://bugs.python.org/issue10544

---

### The surprising comma

Suggested by @MostAwesomeDude in [this](https://github.com/satwikkansal/wtfPython/issues/1) issue.
Expand Down

0 comments on commit 31d4382

Please sign in to comment.