Questions tagged [yield-from]

"yield from" is an expression in Python for delegating to a subgenerator.

The expression yield from was added in Python-3.3 based on PEP 380: Syntax for Delegating to a Subgenerator.

In the simplest case it can be used as a shorthand for iterating over a subgenerator, i.e.

for item in sth:
    yield item

can be replaced with:

yield from sth
59 questions
76
votes
7 answers

Converting "yield from" statement to Python 2.7 code

I had a code below in Python 3.2 and I wanted to run it in Python 2.7. I did convert it (have put the code of missing_elements in both versions) but I am not sure if that is the most efficient way to do it. Basically what happens if there are two…
vkaul11
  • 4,098
  • 12
  • 47
  • 79
30
votes
2 answers

yield from vs yield in for-loop

My understanding of yield from is that it is similar to yielding every item from an iterable. Yet, I observe the different behavior in the following example. I have Class1 class Class1: def __init__(self, gen): self.gen = gen …
erzya
  • 598
  • 4
  • 9
26
votes
1 answer

"yield from iterable" vs "return iter(iterable)"

When wrapping an (internal) iterator one often has to reroute the __iter__ method to the underlying iterable. Consider the following example: class FancyNewClass(collections.Iterable): def __init__(self): self._internal_iterable =…
PeterE
  • 5,715
  • 5
  • 29
  • 51
25
votes
2 answers

What is the correct way to yield from a stream?

I have a Connection type that I’m using to wrap read/write stream pairs from asyncio. class Connection(object): def __init__(self, stream_in, stream_out): self._streams_ = (stream_in, stream_out) def read(self, n_bytes: int = -1): …
Zach Gates
  • 4,045
  • 1
  • 27
  • 51
13
votes
2 answers

Difference between `yield from $generator` and `return $generator`?

I have a function that gives back a generator. At the moment it uses yield from: function foo() { $generator = getGenerator(); // some other stuff (no yields!) yield from $generator; } If I replace that yield from with a simple return,…
flori
  • 14,339
  • 4
  • 56
  • 63
12
votes
2 answers

How to intercept the first value of a generator and transparently yield from the rest

Update: I've started a thread on python-ideas to propose additional syntax or a stdlib function for this purpose (i.e. specifying the first value sent by yield from). So far 0 replies... :/ How do I intercept the first yielded value of a…
Anakhand
  • 2,838
  • 1
  • 22
  • 50
12
votes
1 answer

Difference between `yield from foo()` and `for x in foo(): yield x`

In Python most examples of yield from explain it with saying that yield from foo() is similar to for x in foo(): yield x On the other hand it doesn't seem to be exactly the same and there's some magic thrown in. I feel a bit uneasy about using a…
Christian
  • 25,249
  • 40
  • 134
  • 225
8
votes
3 answers

Modifying yield from's return value

Let's say I have these parsers: parsers = { ".foo": parse_foo, ".bar", parse_bar } parse_foo and parse_bar are both generators that yield rows one by one. If I wish to create a single dispatch function, I would do this: def parse(ext): …
Bharel
  • 23,672
  • 5
  • 40
  • 80
7
votes
3 answers

What does a return do when using a "yield from" expression?

I haven't been able to find any examples of return values from the yield from expression. I have tried this simple code, without success: def return4(): return 4 def yield_from(): res = yield from range(4) res = yield from…
blueFast
  • 41,341
  • 63
  • 198
  • 344
6
votes
1 answer

What kind of objects `yield from` can be used with?

Initially (PEP 380), yield from syntax was introduced to be used for delegating to a "subgenerator." Later it was used with now deprecated generator-based coroutines. I cannot find out what kind of objects yield from can be applied to in general. My…
Alexey
  • 3,843
  • 6
  • 30
  • 44
6
votes
3 answers

Difference between `return iterator` and `yield from iterator`

I'm trying to implement my own version of itertools.compress, the problem is that i stumbled upon the return type. I mean both of these functions return an iterator, but i think the second one is not considered a generator function because there is…
marsouf
  • 1,107
  • 8
  • 15
5
votes
6 answers

Remove consecutive duplicates from a list using yield generator?

i'm trying to compress a list using generator: examples [1, 1, 1, 1, 2, 2, 2, 1, 1, 1] == [1, 2, 1] [5, 5, 5, 4, 5, 6, 6, 5, 5, 7, 8, 0, 0])) == [5, 4, 5, 6, 5, 7, 8, 0] I tried to use a generator that checks if the 1st and 2nd element are equal…
bienology
  • 63
  • 4
5
votes
2 answers

Does `yield from` have O(1) time complexity?

Consider the following code snippet. from typing import Iterable def geometric_progression( start: float, multiplier: float, num_elements: int ) -> Iterable[float]: assert num_elements >= 0 if num_elements > 0: yield start …
CrabMan
  • 1,578
  • 18
  • 37
5
votes
1 answer

Why does nesting "yield from" statements (generator delegation) produce terminating `None` value?

Is it possible to nest yield from statements? The simple form is obvious: def try_yield1(): x = range(3) yield from x Which produces: 0 1 2 But what if I have nested generators? def try_yield_nested(): x = [range(3) for _ in range(4)] …
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
4
votes
2 answers

"yield from" another generator but after processing

How do we yield from another sub-generator, but with transformation/processing? for example: in code below, main_gen yields x after transformation using f(x) def f(x): return 2*x def main_gen(): for x in sub_gen(): yield f(x) can this…
toing
  • 466
  • 1
  • 3
  • 19
1
2 3 4