Home >Backend Development >Python Tutorial >A brief introduction to yield expression in python (with examples)
This article brings you a brief introduction to the yield expression in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The yield expression is used in the generator function
When calling the generator function, an iterator is returned (the statements within the function will not be executed). When the iterator function is called, the yield expression is executed,
The current function pauses execution, returns the value of the expression to the caller, continues to call the iterator function, and resumes execution from the pause point. ,
Encountering a yield expression is similar to encountering other expressions. The yield expression also has a value, usually None.
The difference from other expressions is that the yield expression will return the value of the expression at yield
The official document describes it as follows:
When a generator function is called , it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator’s methods is called. At that time, the execution proceeds to the first yield expression, where it is suspended again, returning the value of expression_list to the generator’s caller. By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. When the execution is resumed by calling one of the generator’s methods, the function can proceed exactly as if the yield expression were just another external call. The value of the yield expression after resuming depends on the method which resumed the execution. If __next__() is used (typically via either a for or the next() builtin) then the result is None. Otherwise, if send() is used, then the result will be the value passed in to that method.
Yield expressions are allowed anywhere in a try construct. If the generator is not resumed before it is finalized (by reaching a zero reference count or by being garbage collected), the generator-iterator’s close() method will be called, allowing any pending finally clauses to execute.
When the underlying iterator is complete, the value
attribute of the raised StopIteration instance becomes the value of the
yield expression.
Official example:
>>> def echo(value=None): ... print("Execution starts when 'next()' is called for the first time.") ... try: ... while True: ... try: ... value = (yield value) ... except Exception as e: ... value = e ... finally: ... print("Don't forget to clean up when 'close()' is called.") ...>>> generator = echo(1)>>> print(next(generator)) Execution starts when 'next()' is called for the first time.1 >>> print(next(generator)) None>>> print(generator.send(2))2 >>> generator.throw(TypeError, "spam") TypeError('spam',)>>> generator.close() Don't forget to clean up when 'close()' is called.
##Simulate a iterator version rangefunction
def my_range(start, stop=None, step=1): if not stop: stop = start start = 0 while start < stop: yield start start += step if __name__ == '__main__': for i in my_range(10): print(i) for i in my_range(0, 10): print(i) for i in my_range(0, 10, 2): print(i)
The above is the detailed content of A brief introduction to yield expression in python (with examples). For more information, please follow other related articles on the PHP Chinese website!