Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add new snippet: Yielding None #45

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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