python - Difference between list comprehension and generator comprehension with `yield` inside -
what difference between list comprehensions , generator comprehensions yield inside? both return generator object (listcomp , genexpr respectively), upon full evaluation latter adds seem rather superfluous nones.
>>> list([(yield a) in zip("abcde", itertools.cycle("12"))]) ['a', '1', 'b', '2', 'c', '1', 'd', '2', 'e', '1'] >>> list(((yield a) in zip("abcde", itertools.cycle("12")))) ['a', '1', none, 'b', '2', none, 'c', '1', none, 'd', '2', none, 'e', '1', none] how come? scientific explanation?
the value of yield from expression none. fact second example generator expression means implicitly yielding iterator, yield value of yield from expression. see this more detailed answer.
Comments
Post a Comment