As a contrived example:
myset = set([\'a\', \'b\', \'c\', \'d\'])
mydict = {item: (yield \'\'.join([item, \'s\'])) for item in myset}
and
I think that yield
is turning your nice dictionary comprehension into a generator expression. So, when you're iterating over the generator, yield is "yielding" the elements that look as
, bs
..., but the statement yield ...
returns None
. So, at the end of the day, you get a dictionary looking like {'a': None, 'b': None, ...}
.
The part that confuses me is why the dictionary is actually yielded at the end. I'm guessing that this behavior is actually not well defined by the standard, but I could be wrong about that.
Interestingly enough, if you try this with a list comprehension, python complains:
>>> a = [(yield i) for i in myset]
File "", line 1
SyntaxError: 'yield' outside function
But it's Ok in a generator (apparently).