Python yield (generator)

Source: Internet
Author: User

The process under Python:

1 #Encoding=utf-82 """3 the process----tiny processes4 Yield Generator-----Generate an iterative object such as list, Tuple,dir5 1, a function containing yield, it is an iterative object (list, tuple, etc.)6 end every run to yield and keep the scene7 2, producer, consumer behavior;8 9 3, do not need to execute immediately, only when neededTen """ One  AA = [1, 2, 3, 4] -  forIinchA: -     PrintI the  - defTest (): -i =0 -A = 4 +      whileI <A: -         """ + 0 A 1 at 2 - 3 -         """ -x =yieldI -i + = 1 -          int =Test () - PrintT#<generator Object Test at 0x0000000002541798> to PrintT.next ()#Next () of the generator + PrintT.next ()#Next () of the generator - PrintT.next ()#Next () of the generator the PrintT.next ()#Next () of the generator * #print T.next () #StopIteration $ Panax Notoginseng PrintType (range (0, 5))#<type ' list ' > - PrintType (xrange (0, 5))#<type ' xrange ' > the  + deftest2 (): Ax =yield  "First , and return" the     Print "First %s"%x +x =yield "Second and return%s"%x -     Print "Second%s"%x $x =yield  $     PrintX#None, no send -      -      thet =test2 () - PrintT.next ()Wuyi PrintT.send ("Try again")#use Send () the value of x is the parameter of send, the x is empty if you do not use send the PrintT.send ("The second args") -  Wu #1 1 2 3 5 8 - Print  "==================" About deftest3 (num): $     ifnum = = 1: -         yieldNum -i = 1 -b = 1 A      whileI <Num: +x =yieldI the          -i = B +I $                  the  the  forIinchTEST3 (13): the     PrintI the  -  in """ the 100 prime numbers after 100000, using yield the """     About defis_p (t_int): the     ifT_int > 1: the          forIinchXrange (2, T_int): the             ift_int%i = =0: +                 returnFalse -         returnTrue the     Else:Bayi         returnFalse the  the defget_primes (): -i = 10000 -      whileTrue: the         ifis_p (i): the             #x = yield I the             yieldI theI +=1 -i + = 1 the  thet =get_primes () the  forIinchXrange (0, 100):94     PrintT.next ()

Python yield (generator)

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.