python3 yield from new knowledge

 

demand

Give you a bunch of string, the output string out by a single element.

Before python3.4, if you want to achieve this demand. We can use a for loop to do

def gennerator():
    a = 'hsadfihdsachdsio'
    b = '2346328452344'
    for i in a:
        yield i
    for i in b:
        yield i
g = gennerator()
for i in g:
    print(i)
View Code

Too long, cut for a while.

but

After python3.4 with a yeild from new

So we can do this

def gennerator():
    a = 'hsadfihdsachdsio'
    b = '2346328452344'
    yield from a
    yield from b
g = gennerator()
for i in g:
    print(i)
View Code

Exactly the same results, but the code is greatly simplified.

 

Guess you like

Origin www.cnblogs.com/zly9527/p/11374681.html