SoFunction
Updated on 2024-12-19

Specific methods for python delegate generators

1, the generator function contains the yield from expression.

2. Suspend the delegate generator at the yield from expression, and the caller can send the data directly to the child generator.

3. The sub-generator sends the output value to the caller.

4. The interpreter throws Stopiteration and attaches the return value to the exception object.

an actual example

# Collaborative code case 1
 
def simple_coroutine():
    print('-> start')
    x = yield
    print('-> recived', x)
    
# Main thread
sc = simple_coroutine()
print(1111)
# You can use (None), which has the same effect.
next(sc) # Pre-excitation
 
print(2222)
('zhuxiao')

Content Expansion

generator

In Python, a function that uses yield is called a generator; unlike a normal function, a generator is a function that returns an iterator, which can only be used for iterative operations, or, more simply, an iterator; during a call to a generator, each time it encounters a yield, the function pauses and saves all of the current runtime information, returns the value of the yield, and The next time the next() method is executed, it continues from the current position; calling a generator function returns an iterator object.

Generating Fibonacci series using generators

to this article on the specific methods of python delegate generator article is introduced to this, more related python how to delegate generator content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!