Yield and return in python-iterator and generator

Summary

What is the difference between yield and return? What is the generator? What is iterator again? What do they do? Simply understand, yield=return, return the result of the function body! Yield itself is a generator, so what we return with return is some of our common objects (eg:list, dict, etc.), what we return with yield is an iterator object!

yield和return

In the previous article [Starting the Scrapy Crawler Road] , when processing items, define the yield used in the return value of the function. Someone asked what yield does and what does it mean?

Similarities: Both are the return value during the definition of the function

difference:

  • Yield is the pause function, return is the end function; that is, the code in the function body will continue to be executed after the return value is returned, and the code in the function body will not be executed after the return value
  • Yield returns an iterator (yield itself is a generator-generator is used to generate iterators); return returns a normal iterable object (list, set, dict and other storage objects with actual memory addresses)
# debug测试
def f1():
    print("第1次暂停")
    yield '第1次访问返回的结果'# yield 暂停函数的调用,返回一个结果
    print("第2次暂停")
    yield '第2次访问返回的结果'
    return 100 # reutrun结束函数的调用
    print('return直接结束,此句不执行')

Builder

If the list elements can be calculated according to a certain algorithm, can we continue to calculate the subsequent elements in the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this mechanism of calculating while looping is called generator: generator.

There are many ways to create a generator. The first method is very simple, just change the [] of a list comprehension to () to create a generator:

>>> L = [x * x for x in range(10)]
>>> L
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> g = (x * x for x in range(10))
>>> g
<generator object <genexpr> at 0x1022ef630>

Insert picture description here
What yield returns is a generator (it is better understood as an iterator)
Insert picture description here

Iterator

The objects that can directly act on the for loop are collectively called iterable objects: Iterable

  • One is the collection data type, such as list, tuple, dict, set, str, etc.;

  • One type is generator, including generator and generator function with yield
    Insert picture description here

The iterator object is accessed from the first element of the collection until all the elements are accessed. The iterator can only go forward and not go backward.

There are two basic methods for iterators: creating iterators iter() and accessing iterators next().

contact

Yield is a generator function, which returns an iterator (it is said that it returns a generator, I think it is more suitable to say that it is an iterator),

Recently, my thinking was confused, and I wrote several blogs, but they all felt bad! It's all turned into a draft. I wanted to save this one as a draft. However, I still want to “GEEK+”原创·博主大赛struggle, so I sent out a ticket. I will come back to revise and improve it when I wake up the following day. Please help me with the voting link at the end of the article.

Originality is not easy, thank you for your support

Here are some information, you can study it yourself if you need it! !

[Python3 Iterator and Generator]
[Iterator-Baidu Encyclopedia]
[Detailed explanation of the usage of yield in python-the simplest and clearest explanation]
[Generator-Liao Xuefeng]
[Iterator-Liao Xuefeng]


Written in the final canvassing session, I
recently participated in the official CSDN organization. “GEEK+”原创·博主大赛
After repeated screenings, it
is not easy to finally make the TOP 50 original list . Help vote for a free vote.
Support: Click to vote.
Thank you! ! !

Guess you like

Origin blog.csdn.net/qq_35866846/article/details/108772968