Python generators in-depth understanding of

1, the generator

(1) What is a generator?

Core: the nature of the generator is a iterator

Iterator is built-in

Builder programmer to write an iterator

def func():
    print("这是一个函数")
    return "函数"
func()

def func():
    print("这是一个生成器")
    yield "生成器"

(2) the preparation of the pattern generator:

<1> based on a function written
<2> derivations prepared

(3) a generator function:

Constructed by the generator function generator

<1> yield representative of the body appears to declare a function generator (generator - Generator)

<2> corresponds to a yield a next ()

def func():
    msg = input("请输入内容")
    yield msg
    print("这是第二次启动")
    yield "生成器2"
    yield "生成器3"
    yield "生成器4"

g = func()     
print(next(g))    
print(next(g))
print(next(g))
print(next(g))   

 # 生成器的本质就是迭代器.迭代器如何取值,生成器就如何取值

<3> yield and return the difference between:

1> same point:
Content is returned
You can return multiple, but the return will only execute a written multiple
2> differences:
return termination function generator yield is suspended
yield the current execution location can be recorded

Action (4) generator: space-saving

(5) usage scenarios:

When a large amount of data files or containers, it is recommended to use the generator

(6) What is the difference between iterators, what is the builder?

<1> through the address:

Iterator address: <list_iterator object at 0x000000987B6E97F0>

Address generator: <generator object func at 0x00000087C2A10CA8>

<2> by a send () method: (send () method must first send None)

Do not send () method is iterator
With a send () method is the generator
def func():
    a = yield 1
    print(a)
    b = yield 2
    print(b)

g = func()
print(g.send(None))   # send  -- 发送
print(g.send("alex"))   # send  -- 发送
print(g.send("宝元"))   # send  -- 发送
. 1> Send and next () difference:

The same point:

send and next () so that the generator can yield a corresponding downward once.

Yield value can be obtained is generated.

Differences:

The first acquisition yield values ​​can only be used next can not send (can send (None)).

can send to yield a set value is transmitted.

(7) yield and the yield from the difference between:

The yield iterables a one-time return

yield from the iterable returned one by one

def func():
    def foo():
        print(11)
    lst = {"key":1,"key1":2}
    yield foo

print(func().__next__())

def func():
    lst = [1,2,3,45,6]
    lst1 = ["alex","wusir","taibi","baoyuan"]
    yield from lst
    yield from lst1

g = func()

for i in g:
    print(i)

lst = [1,2,3,45,6]
lst1 = ["alex","wusir","taibi","baoyuan"]

for i in lst,lst1:
    print(i)

(8) iterables, comparator iterators, generator:

Iterables

Advantage: list, tuple .str save time, convenient value, flexible (with its own private method)

Disadvantages: consume a lot of memory

Iterator

Benefits: save space

Disadvantages: can not directly see the value, use inflexible, time-consuming, one-time, non-retrograde

Builder

Advantages: saving space, arbitrarily defined

Disadvantages: can not directly see the value, time-consuming, one-time, non-retrograde

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160552.htm