Python iterator, generator and decorator, and python generator Decoration

Source: Internet
Author: User

Python iterator, generator and decorator, and python generator Decoration

1. Principles and usage of the iterator:
1> principle:
An iterator is a way to access a set element. The iterator object is accessed from the first element of the set until all elements are accessed. The iterator can only move forward and not backward, but that's nothing. People seldom step back on the iteration process. In addition, the major advantage of the iterator is that they do not need to prepare all the elements in the entire iteration process in advance; the iterator calculates the element only when it iterates to an element. Before or after this, the element may not exist or be destroyed; this feature makes it especially suitable for Traversing large or infinite sets, such as several GB of files;
Features:
(1) visitors do not need the internal structure of the relational iterator. They only need to use the next () method to retrieve the next content;
(2) A value in the set cannot be randomly accessed, but can only be accessed from start to end in sequence;
(3) Half of the access requests cannot be returned;
(4) easy to collect large data sets and save memory;
2> Generate an iterator: 3. Use _ next _ () to view the iterator;

2. generator ):
Definition: When a function is called, an iterator is returned. This function is called a generator. If the function contains the yield syntax, the function will become a generator;
Yield:
This can interrupt the function and save the interrupt status. After the interruption, the code can continue to be executed. After a while, you can call the function again to execute the function from the next yield sentence;
1> generator generation:

2> use yield to Implement Asynchronous concurrency in multiple threads:

2. decorator: expands new functions for implemented functions;
1> decorator without parameters;

2> A parameter-like decoration device;

3> decorator:

4> implement a complex decorator with parameters: a function is decorated by multiple decorator;

Process Analysis:

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.