[Oldboy-django] [4python interview] about yield those things

Source: Internet
Author: User
Tags generator generator generator

1 yield in use Send, next time difference (example m = Yield 5)

Whether Send,next first understood that M = yield 5 is the result of the expression "yield 5" is returned to M, instead of m=5 so simple, it involves how to get the value of the expression yield 5, which is related to Next,send.

When send (ARG), the result of ARG as the yield 5 expression is assigned to M, and if Next, none is paid to m as the result of the yield 5 expression;

So you can think of next equivalent to send (None), except for the first time next

  

The difference is:

With Next (), the first time next, when the yield stop is encountered, and the expression "5" to the right of yield is returned to the outside world as the return value of the next function; Subsequent next will assign none as the value of yield 5 expression to M,

The program executes down, and when it encounters the next yield, the expression 5 on the right side of the yield is the return value of next.

When using Send (ARG) (you must have already used next), Arg is assigned to m as the yield expression "yield 5", the program executes down, and the right expression "5" of the next yield is the return value of the Send function.

  

Next Example:

deff ():Print("Start") Current=yield "Hello"    Print('current=', current) whileTrue:value=yield " Bad"        #value = value + ' Not ' # This line will error because value is none and cannot be added to the stringg=f () S1=Next (g)Print('s1=', S1)#The first time next, stop on line 5th, and the expression (none or none) on the right side of yield as the return value of next ()S2=Next (g)Print('s2=', S2)#The second next will assign None as yield expression "yield hello" value to current, that is, current = None#The program then executes, encountering the yield "bad" stop, and the expression "bad" on the right of yield as the return value of the second nextS3=Next (g)Print('s3=', S3)#The third next will assign None as the value of the yield expression "yield bad" to value, that is, value = None,#The program executes down, encounters value = yield "bad" stop, and "bad" as the third return value of next
View Code

Send example:

deff ():Print("Start") Current=yield "Hello"    Print('current=', current) whileTrue:value=yield " Bad"        Print("value=", value)#value = value + ' Not ' # This line will error because value will be shaped and cannot be added to the stringg=f () S1= Next (g)#This step can not be less#The first time next, stop on line 5th, and the expression (none or none) on the right side of yield as the return value of next ()Print(G.send (10))#when the generator calls the first send, it assigns the send parameter 10 instead of the value of the expression "yield hello" to the current,#The program then executes and returns the return value of "bad" as the first send () function when it encounters a yield bad stop.Print(G.send (20))#when the generator calls send for the second time, it assigns the send parameter 20 instead of the expression "yield bad" to value#The program executes down and then encounters a yield bad stop, returning the "bad" as the return value of the second send () function to the outside world
View Code

   

2 yield and generator generator

The function uses yield no longer as a function, but as a generator generator. Like what

def Fab (max):       = 0, 0, 1       while n < Max:          yield  b          #  print B          A, B = B, A + b          = n + 1

When you execute G = Fab (5), instead of calling a function, you instantiate a generator object. The code inside the FAB function is called when the G object or Next (g) is looped

defTest ():#print (' one ')    yield1#print (' both ')    yield2g=Test () forIinchg:Print(i)#should be the same as the following effect:g =Test () Res=Next (g)Print(RES)#The first time you encounter next () executes the test code and stays at yield 1,#return the value of the expression after yield as the return value of next to the outside#res =Next (g)Print(RES)#next to the saved place, run print#hit yield 2, stop and return the following 2 as the return value of next to the outside world
View Code

The 3 generator is stateful, and the caller cannot use it repeatedly. Like what:

def Test ():     yield 1    yield 2= Test () for in G:      Print(i)#  below here is no effect for the in   g:      Print('hello')    print(i)
View Code

  

[Oldboy-django] [4python interview] about yield those things

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.