python - Why does the generator send method print results to the screen without a call to the print function? -


see console session below:

>>> def f(x): ...  _ in range(10): ...   y = yield ...   x.append(y) ...   yield x ... >>> x = [] >>> g = f(x) >>> _ in g: ...  g.send(1) ... [1] [1, 1] [1, 1, 1] [1, 1, 1, 1] [1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 

note .send() method causing generator output printed screen. believe .send() method causing because if generator output none, nothing printed screen (which usual case none).

>>> x = [] >>> g = f(x) >>> in g: ...  g.send(1) ... >>> x [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 

additionally, if iterate through without sending input, there nothing printed screen (occurs either version of f above):

>>> x = [] >>> g = f(x) >>> in g: ...  pass ... >>>  

can explain why output screen happens? thinking there interesting going on here don't know is.

to clarify, surmise there nothing complicated going on here happens when this:

>>> [] [] 

the console repeating result of expression. why console behaving if there has been repeated expression entry in case above?

from python docs:

the send() method returns next value yielded generator, or raises stopiteration if generator exits without yielding value.

each time call send() method, returning next value yielded generator.

in interactive terminal, these returned results printed screen. nothing magical.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -