Python iterator and generator and decorator, and python generator Decoration

Source: Internet
Author: User

Python iterator and generator and decorator, and python generator Decoration

1. Iterator)

An iterator is a way to access collection elements. It has the following features:

1) Each call to the _ next _ () method only accesses one element and cannot be used back. This facilitates large data sets and saves memory; (When no element is accessible in the container, the next () method will throw a StopIteration exception to terminate the iterator)

2) It can only be accessed from start to end and cannot be randomly accessed;

3) The iterator provides a unified interface for accessing the set. As long as the iter () method object is defined, the iterator can be used for access.

Iterator usage:

Lis = ['A', 'B', 'C', 'D'] a = iter (lis) print (. _ next _ () print (. _ next _ () print (. _ next _ () print (. _ next _ () print (. _ next _ () # No element is obtained, resulting in an error in StopIteration # running result abcdTraceback (most recent call last): File "F: /Python/pythoncode/s12/study/study5.py ", line 318, in <module> print (. _ next _ () StopIteration


2. Generator)

A function that calls the return iterator is called a generator. The function contains the yield syntax, and this function will become a generator.

1) generator expression

Used to generate a regular Generator

Format: generator_name = ('generate the rule' for I range (num) if 'I' condition ')

1 a = ('A' for I in range (5) if I % 2) 2 B = (I + 1 for I in range (5) 3 print (, 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 __()) 10 11 # running result 12 <generator object <genexpr> at 0x00F9CAE0> <class 'generator'> 13 <generator object <genexpr> at 0x00F9CC90> <class 'generator'> 14 115 216 317 418 5
Generative expression

2) yield creates a generator.

Def fun_ex (a): yield 1re = fun_ex (2) print (re. _ next _ (), type (re) # Run result 1 <class 'generator'>

3) The generator achieves the asynchronous concurrency of a single thread.

Def gen_ex (a): while a> 0: a-= 1 yield 1 print ('hello') re = gen_ex (2) print (re. _ next _ () print ('interrupt print ') print (re. _ next _ () # Run result 1 stop printing hello1

The generator returns data once every call, so other operations can be inserted in the middle to form an asynchronous effect. In this example, "interrupt printing" is first introduced"

4) use the send () method in the Generator

Def gen_ex (a): while a> 0: a-= 1 B = yield print (B) re = gen_ex (5) re. _ next _ () re. send (5) print ('interrupt print ') re. send (6) # Run result 5 interrupt print 6

Send () can send parameters to yield, and yield is used as the receiver. Here, the yield running condition is different from the return function.

If the generator uses a _ next _ () method, it runs to the yield line and stops. However, using the send () method, the function runs directly from the yield line and assigns the value to B. Then, the function runs continuously until the yield line stops.

 

5. decorator Principle

6. decorator implementation

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.