3

Now I'm studying on the differences between a yield-from and an await syntax. From the official python documentation, the yield-from generator() is just a syntax suger of the following code:

for i in generator(): yield i

But I can't desugar the yield-from in the example below.

def accumlate():
    # context
    accumlator = 0
    while True:
        next = yield
        if next is None:
            return accumlator
        accumlator += next


def gather(tallies):
    while True:
        tally = yield from accumlate() # (*)
        tallies.append(tally)

def main():
    tallies = []
    accumlator = gather(tallies)
    next(accumlator)
    for i in range(4):
        accumlator.send(i)

    accumlator.send(None)
    for i in range(6, 10):
        accumlator.send(i)
    accumlator.send(None)
    print(tallies)

if __name__ == "__main__":
    main()

I tried to just replace a yield-from with the for-in version, but it did't work because the for-in can't be placed on the right side of the tally variable. What is an exact desugar of the code marked with asterisk ?

BrainVader
  • 383
  • 1
  • 3
  • 7

2 Answers2

1

@DerteTrdelnik's answer is largely correct, except that you don't have to modify the accumlate function at all, since a generator would already automatically raise StopIteration with the returning value as a parameter to construct the exception object when the generator returns without a yield.

Excerpt from the documentation of StopIteration:

When a generator or coroutine function returns, a new StopIteration instance is raised, and the value returned by the function is used as the value parameter to the constructor of the exception.

Therefore, you only need to "desugar" the gather function as such:

def gather(tallies):
    while True:
        a = accumlate()
        a.send(None)
        while True:
            try:
                a.send((yield))
            except StopIteration as e:
                tallies.append(e.value)
                break
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

result = yield from generator() is not easily substituted, it is the way of how to get to the return value of generator

see https://www.python.org/dev/peps/pep-0380/#proposal

to simulate a bit of what is going on without using yield from we have to modify both of the generators

def accumlate():
    # context
    accumlator = 0
    while True:
        next = yield
        if next is None:
            raise StopIteration(accumlator)
        accumlator += next


def gather(tallies):
    internal_acc = accumlate()
    internal_acc.send(None)
    while True:
        try:
            number_to_add = yield
            internal_acc.send(number_to_add)
        except StopIteration as e:
            internal_acc = accumlate()
            internal_acc.send(None)
            tallies.append(e.value)

accumlate is no longer returning, but raising and gather has to try except that raise,

after internal_acc is exhausted a new one is created in the except

there is a bit more to it as seen in the pep0380, but basically generator return is a raise and yield from is a compact catch for that

Derte Trdelnik
  • 2,656
  • 1
  • 22
  • 26