Python - what makes an income?

Since python 2.5 has the ability to send(), throw(), close()to the generator. Inside a specific generator, you can "catch" the data sent by doing something like:

def gen():
    while True:
        x = (yield)
        if x == 3:
            print('received 3!!')
            break
        else:
            yield x

What I'm trying to play does something like:

def gen2():
    while True:
        yield (yield)

I noticed that this is a legitimate generator that does something .. The first thing I'm trying to understand is a good use for such a letter. Glad to help here.

Also when you do something like:

g = gen2()
next(g)
g.send(10) # output: 10
g.send(2) # output: nothing
g.send(3) # output: 3
g.send(44) # output: nothing

Why doesn't every second β€œsend” do anything?

+4
source share
2 answers

yield (yield) None yield. send next. yield , yield .


yield :

  • send next.
  • send next.

, send next :

  • yield, . ( None next.)
  • yield.

, , , . a yield send next, a send next yield s.

:

def gen():
    yield (yield)

g = gen()
print(next(g))
print(g.send(1)
g.send(2)

:

Inside the generator                      Outside the generator
                                          next(g) sends None to g
g receives None, ignores it
  (since it paused at the start
   of the function)
g executes the "transmit" phase
  of the inner yield, transmitting
  None
                                          next(g) receives None
                                          g.send(1) sends 1 to g
g executes the "receive" phase
  of the inner yield, receving 1
g executes the "transmit" phase
  of the outer yield, transmitting 1
                                          g.send(1) receives 1 from g
                                          g.send(2) sends 2 to g
g executes the "receive" phase
  of the outer yield, receiving 2
g reaches the end of gen and raises
  a StopIteration
                                          g.send(2) raises the StopIteration
                                          from g
+5

yield - . - , .send, None, ( , next .send). .send - , , , , . , , .send, ( None), , yield, ( None).

:

def gen():
    sent1 = yield 1
    print(sent1, "was sent")
    sent2 = yield 2
    print(sent2, "was sent")
    print("Reached end of generator")

g = gen()
print(next(g), "was yielded")
print(g.send("A"), "was yielded")
print(g.send("B"), "was yielded")
next(g)

# output
1 was yielded
A was sent
2 was yielded
B was sent
Reached end of generator
# StopIteration is raised here

next None, yield yield (yield) (.. ). send 10 yield. , send, . , send , , , None. , send, ; None , . , , . , send yield, yield . ( , , script, , print .)

, :

def gen():
    yield (yield (yield (yield "WHOA")))

>>> g = gen()
>>> next(g)
'WHOA'
>>> g.send(1)
1
>>> g.send(2)
2
>>> g.send(3)
3
>>> g.send(4)
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    g.send(4)
StopIteration

, , , . , yield yield. yield "" yield . , , StopIteration .

. , , , send " " . . send , next. next(gen) gen.send(None).

+2

Source: https://habr.com/ru/post/1684545/


All Articles