Python iterators and generators and adorners

Source: Internet
Author: User

1. iterators (Iterator)

Iterators are a way to access the elements of a collection. Has the following characteristics:

1) Each call to the __next__ () method accesses only one element, and can not back up, easy to loop over large data collection, save memory; (when there are no accessible elements in the container, the next () method throws an stopiteration abort iterator)

2) can only access from beginning to end, cannot randomly access a value;

3) Iterators provide a unified interface to access the collection, as long as the ITER () method object is defined and can be accessed using an iterator.

Iterators using:

lis=['a','b','C','D']a=ITER (LIS) print (a.__next__ ()) print (a.__next__ ()) print (a.__next__ ()) print (a.__next__ ()) Print ( a.__next__ ()) # No element obtained, resulting in stopiteration error # Run result Abcdtraceback (most recent call last): File"f:/python/pythoncode/s12/study/study5.py", line318,inch<module>print (a.__next__ ()) Stopiteration


2. Generator (Generator)

A function called the return iterator is called the generator. The function contains the yield syntax, which becomes the generator.

1) Generator expression

Used to generate a regular generator.

Format: generator_name= (' Generate rule ' for I range (num) If ' I's condition ')

1A= ('a'  forIinchRange5)ifi%2)2B= (i+1  forIinchRange5))3 Print (A,type (a))4 Print (B,type (b))5 print (b.__next__ ())6 print (b.__next__ ())7 print (b.__next__ ())8 print (b.__next__ ())9 print (b.__next__ ())Ten  One #运行结果 A<generatorObject<genexpr> at0x00f9cae0> <class 'Generator'> -<generatorObject<genexpr> at0X00F9CC90> <class 'Generator'> - 1 the 2 - 3 - 4 - 5
built-in expressions

2) yield Create generator

def FUN_EX (a):     yield 1re=fun_ex (2)print(re.  __next__(), type (re))# Run Results 1 <class'generator  '>

3) generator implements single-threaded asynchronous concurrency effects

defGEN_EX (a): whileA>0:a-=1yield1Print('Hello') Re=GEN_EX (2)Print(Re.__next__())Print('Interrupt Printing')Print(Re.__next__())#Run Results1Interrupt Print Hello1

The generator returns data once per call, so you can insert other operations midway through to form an asynchronous effect, such as "Break print" in the example first

4) The Send () method in the generator uses the

def GEN_EX (a):      while a>0:        a-=1        b=yield        print(b) Re=gen _EX (5) re. __next__ () re.send (5) Print (' interrupted printing ') re.send (6) # Run result 5 interrupt Print 6

Send () can pass the yield parameter, yield as the receive. There is some difference between the performance of yield and the function of return.

If the generator uses a __next__ () method, it will run to the yield line and stop. But then using the Send () method, the function runs directly from the yield line and assigns a value to B, and then runs down the loop once to the yield line to stop.

5. The principle of the adorner

6. The implementation of the adorner

Python iterators and generators and adorners

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.