Python iterators, generators and adorners

Source: Internet
Author: User
Tags generator

1 "The principle and use of iterators:
1> principle:
Iterators are a way to access the elements of a collection, and the iterator object is accessed from the first element of the collection until all the elements have been accessed, and the iterator can only move forward without going backwards, but that's fine, and there's little to go backwards in the iteration, and one of the great advantages of iterators is that they don't require preparation for the entire iteration An iterator that computes an element only when it iterates over an element, before or after which the element may not exist or is destroyed; This feature makes it particularly useful for traversing large or infinite collections, such as several G files;
Characteristics:
(1) The visitor does not need the structure within the relational iterator, but only needs to go through the next () method to fetch the content;
(2) Can not randomly access a value in the collection, can only be accessed from beginning to end;
(3) can not return to half of the visit;
(4) Easy to cycle relatively large data collection, save memory;
2> generates an iterator: 3 using __next__ () to view;

2 "Generator (generator):
Definition: When a function call returns an iterator, the function is called the generator, and if the function contains the yield syntax, the function becomes the generator;
The main role of yield:
You can break the function and save the interrupt state, the code can continue to execute after the break, and after a period of time it can be recalled again, starting from the next sentence of the last yield;
Generation of the 1> generator:

2> uses yield to implement asynchronous concurrency effects in multiple threads:

2 "Decorator: Extend new functions to the functions that have been realized;
1> a similar adorner without parameters;

A similar adorner with 2> parameters;

3> Decorator:

4> implements a complex adorner with parameters: a function is decorated with multiple adorners;

Process Analysis:

Python iterators, 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.