From e2c2cf106b954f5388df8f6d38601cb0140c95af Mon Sep 17 00:00:00 2001 From: Satwik Kansal Date: Wed, 11 Oct 2017 17:42:45 +0530 Subject: [PATCH] Add new snippet: Yielding None Closes https://github.com/satwikkansal/wtfpython/issues/32 --- README.md | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 670a1fb..221e03b 100755 --- a/README.md +++ b/README.md @@ -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) @@ -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] + 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.