Python interview questions twenty-five (comparison of yield and return)

Similarities: all return the result of function execution

Difference: return ends the function after returning the result, and yield turns the function into a generator. The generator generates a value (yield statement) each time, the function is frozen, and a value is generated after being awakened.

to sum up:

  • In the usual for ... in ... loop, after in is an array. This array is an iterable object, similar to linked lists, strings, and files. It can be mylist = [1, 2, 3], or it can be mylist = [x * x for x in range (3)]. Its drawback is that all data is in memory, and if there is a large amount of data, it will consume a lot of memory.
  • The generator can be iterated, but it can only be read once. Because it is only generated when used. For example, mygenerator = (x * x for x in
    range (3)), note that () is used here, it is not an array, and the above example is [].
  • The key to the generator that I understand is that it can iterate because it has a next () method, which works by repeatedly calling the next () method until an exception is caught. You can use the mygenerator test above.
  • The function with yield is no longer an ordinary function, but a generator generator, which can be used for iteration. The working principle is the same as above.
    Yield is a keyword similar to return. When the iteration encounters a yield, it returns the value after the yield. The point is: the next iteration begins with the code following the yield encountered in the previous iteration.
  • Yield is that return returns a value, and remember the position of the return, the next iteration will start from this position.
  • The function with yield is not only used in the for loop, but also can be used for the parameters of a function, as long as the parameters of this function allow iteration parameters. For example, the array.extend function, its prototype is array.extend (iterable).
  • The difference between send (msg) and next () is that send can pass parameters to the yield expression. The parameters passed at this time will be used as the value of the yield expression, and the yield parameter is the value returned to the caller. ——In other words, send can forcibly modify the value of the last yield expression. For example, there is a yield assignment in the function, a = yield 5, the first iteration will return 5 here, a has not been assigned. In the second iteration, use .send (10), then, it is to force the yield 5 expression to be 10, which is originally 5, then a = 10
  • Both send (msg) and next () have return values. Their return value is the value of the expression after the yield when the current iteration encounters the yield. In fact, it is the parameter after the yield in the current iteration.
  • The first call must be next () or send (None), otherwise an error will be reported, and the reason why it is None after send is because there is no previous yield at this time (according to Article 8). It can be considered that next () is equivalent to send (None).
Published 56 original articles · praised 0 · visits 1260

Guess you like

Origin blog.csdn.net/weixin520520/article/details/105487868