Advanced features generator

Not only can the generator output value, the caller can also receive the reference

 

for example

DEF gen_fun ():
     # in this way, can the value of output, may also be received call parameters passed in 
    HTML = the yield  " http://www.baidu.com " 
    Print (HTML)

    # This way you can only output value, can not receive the caller into the reference 
    yield  " ha ha " 
    yield  " quack "

if __name__ == '__main__':
    Gen = gen_fun ()
     Print (Next (Gen)) # Next method to obtain an output value generated 
    send_result gen.send = ( ' I'm HTML ' )   # Send method, the parameters passed to the internal generator, but also can restart generator, after executing the content yield 
    Print (send_result)

 

Print Results:

http://www.baidu.com
I'm html
Haha

 

All in the code printing.

next (): Get the value of output generator 

send (): The parameters passed to the internal generator, the generator while the restart, return the next value for a yield

 

 

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/12089145.html