Generator in Python generator

Source: Internet
Author: User

Generator (Generator)

1. Generator is a container

2, the generator can be the edge of the loop calculation

3. Generator can save memory

4. The generator is an iterative object

5. The function that becomes generator, executes at each call, next() encounters a yield statement return, executes again from the last statement returned yield

Generation of generators

1, generator expression like list as

 for inch [1,2,3,4]]

The difference is that the generator is ()

 for  in range (4))
 for  in range (4))print(Next (g)) for in G:      Print(x)

2. Generator function yield

  

def fib (max):    N, a, b = 0, 0, #    L = [] while    n < max:#        print (b)        yield b#        l.append (b) 
   a, B = B, a + b        n = n + #    return L    return ' done ' F = fib (6) print (f) print (Next (f)) print (next) (f))

Generator in Python 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.