Introduction to the functions of generators and iterators in Python

Source: Internet
Author: User
Tags iterable

The features of the generator and iterators are described in 1. Generator (generator) 1. Assignment Generator 1. Create
方法:x = (variable for variable in iterable)例如:x = (i for i in range(10))     print(x) >>> <generator object <genexpr> at 0x00000000006B85C8>返回值:generator#使用元祖推导式的时候回变成一个生成器。
2. Call
方法:x.__next__()返回值:object#对应生成器一般使用该种方法调用,当然也可以通过for循环进行遍历。
2. Function Builder

Function generators are also commonly used or commonly said generators, as the name implies is created by the function, but in the function must use the Yield keyword declaration.

1. Yield as the return value

Fibonacci Sequence:

  1. General function implementation

      For example: Def Fei (max_n): N,a,b = 0,0,1 while n < Max_n:print (b) A, B = b,a+b n + = 1 return ' Game Over ' #通过一个函数实现斐波那契数列.                        The results can be printed directly by FEI (NUM) calls. 
  2. Generator implementation

    例如:def fei(max_n):         n,a,b = 0,0,1         while n < max_n:             yield b             a,b = b,a+b             n += 1         return ‘game over‘#通过一个生成器斐波那契数列。次数函数执行的结果则为一个生成器,因此调用时需要对函数的执行结果赋值给变量,然后通过__next__()方法进行调用。
2. Yield as the receiving value

Single Thread asynchronous parallel

例如:import time     def consumer(name):         print(‘%s来吃包子了!‘%name)         while True:             bao = yield             print(‘包子%s被%s吃了!‘%(bao,name))     def producer():         zhang = consumer(‘zhangsan‘)         li = consumer(‘lisi‘)         zhang.__next__()         li.__next__()         print(‘包子已经在锅里了!‘)         for i in range(1,6,2):             j = i+1             time.sleep(1)             print(‘包子%s好了!‘%i)             print(‘包子%s好了!‘%j)             zhang.send(i)             li.send(j)     producer()#把yield作为接收值,使用send方法把数据传递给yield来执行生成器。     
2. iterators (iterator)

Objects that can be iterated through a for loop are iterative objects for data types STR, list, tuple, dict, set, generator, which are iterable, where generator can use next() An iterative object that the method invokes. The corresponding iterator is defined as an iterator that can be invoked using the next() method. So the generator is one of the iterators. The other data types that correspond to these are just an iterative object, not an iterator, but you can convert the other data types to an iterator through the ITER () method.

Introduction to the functions of generators and iterators in Python

Related Article

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.