The Send method of "Python Learning ten" yield

Source: Internet
Author: User
Tags iterable

Yield effect

Simply put, the function of yield is to turn a function into a generator, the function with yield is no longer a normal function, and the Python interpreter treats it as a generator. Here's a description of the Fibonacci sequence:

#Common function implementationsdeffib (max): N, a, b= 0, 0, 1 whileN <Max:Print(b) A, B= B, A +B N= n + 1return ' Done'"""Insufficient: Printing a number with print in the FAB function results in poor reusability of the function because other functions cannot obtain the sequence generated by the function. """ #returns a Listdeffib (max): N, a, b= 0, 0, 1L= []     whileN <Max:l.append (b) A, B= B, A +B N= n + 1returnL"""the memory that the function consumes in the run will increase with the parameter max, if you want to control the memory consumption, it is best not to use list to save the intermediate result, but to iterate through the Iterable object. """ #Use yielddefFab (max): N, a, b= 0, 0, 1 whileN <Max:yieldb#Print BA, B = B, A +B N= n + 1"""A function with yield is no longer a normal function, and the Python interpreter treats it as a generator,
Calling Fab (5) does not execute the FAB function, but instead returns a Iterable object"""

The following is a yield version of the FAB function:

# Run  for  in Fab (5):    print(n)#  result 11235

When the For loop executes, each loop executes the code inside the FAB function, and when it executes to yield B, the FAB function returns an iteration value, and the next iteration, the code proceeds from the next statement of Yield B, and the local variable of the function looks exactly the same as before the last break, so the function Continue execution until yield is encountered again.

Yield Send method

The Send method in the generator is not understood before and only one of the next methods is known. Use the following code example to record your understanding:

defFunc1 ():#Generator Functionsx =yield1Print('This are x in func1:', x) x=yieldx#print (' This is x: ', x)F1=func1 ()Print('This is next (F1):', Next (F1))#when the next (F1) method is called, Python first executes the yield 1 statement of the Func1 method#The next method returns the value of the expression after the yield keyword, which is 1#at this point the method func1 interrupt execution, assignment of X and subsequent statements, no longer executing#execute the Send method or the next method:Print("This is f1.send (' E '):", F1.send ('e'))#print ("Second Next:", Next (F1))#When the f1.send (' E ') method is called, the break caused by yield before recovery#Continue execution x = yield 1, assign value to x#The return value at this point (yield 1) is the parameter value ' E ' of the Send method and assigns it to x#then proceed to print (' This was x in Func1: ', x)#The result of the execution is that is x in Func1:e#when calling next method->print ("Second Next:", Next (F1))#Func1 will continue to run from the previous Interrupt->yield 1 statement#the statement print (' This was x in Func1: ', x) will be executed. #However, the return value of yield 1 at this point is none and assigns it to x#Therefore, the result of the execution is: the is x in Func1:none#If you call the F1.send (' E ') method, then continue execution, you will encounter the yield x statement#Func1 is suspended again. #at this point, the return value of the Send method is the value of the expression after the yield keyword, which is the value of x ' E '#the execution results are: This is f1.send (' E '): E#if there is no yield x statement within the Func1 method, an error will be added: Stopiteration#If you call the Send method again at this time#print ("This is f1.send (' F '):", F1.send (' F '))#at this point the return value of the expression (yield x) is defined as the value of the send method parameter, which is ' F '#Next x = yield x This assignment statement will set the value of X to ' F '#continue running, func1 method execution complete, throw stopiteration exception#f1.send (' F ') also has no return value. #However, we can add a statement after the Func1 method, x = yield x statement#print (' This was last x: ', x), used to test the value of X ' F '

In summary, the difference between the Send method and the next method is that when the Send method is executed, the parameters within the Send method are first assigned to the return value of the last pending yield statement. However, it is important to note that the Execute send method will cause an error if no yield statement is pending until a generator object executes the next method:

typeerror:can't send non-none value to a just-started generator

However, the following statement is possible:

F2 = func1 ()print("", F3.send (None)) # Run result
# Send None Value: 1

When the Send method has a parameter of none, it is completely equivalent to the next method. But note that although the above code is acceptable, it is not canonical. Therefore, it is better to call the next method first before calling the Send method.

The above, is that I find the data, to understand the yield of the Send method of the process and experience. Thanks: 56293173 for the reference provided.

The Send method of "Python Learning ten" yield

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.