Python iterator generator (1), python Generator

Source: Internet
Author: User

Python iterator generator (1), python Generator

A for loop can be used for any sequence type in python, including sequences, tuples, and strings. For example:

>>> For x in [1, 2, 3, 4]: print (x * 2, end = '')
...
2468

>>> For x in (1, 2, 3, 4): print (x * 2, end = '')
...
2468

>>> For y in 'python': print (y * 2, end = '')
...
Pp yy tt hh oo nn

In fact, a for loop is even more common than this: it can be used for any iteratable object. We can think of for as an iteration tool. There are also some examples, such as list parsing, in member relationship testing, and built-in map functions.

File iterator

The file has a method named _ next __. each call will return the next line in the file. It is worth noting that when the end of the file is reached, __next _ will cause a built-in StopIteration exception, rather than returning an empty string.

For example:

Note that the print here uses end = ''to add a \ n all the time, because the row string already has one (if this is not found, our output will be separated by two rows ),

In this way, there are three advantages for reading files:

1. Simple Writing

2. Fast Running

3. memory usage is also the best

The original method with the same effect is to call the readlines method of the file in a for loop, which is to load the file into the memory and form a list of line strings.

Although the two effects are the same, the latter Loads files to the memory at a time. If the file is too large, the computer memory space is insufficient, or even cannot work. The former version of the iterator has the immune function for this problem. (Python3 rewrite I/o to support unicode text to make this a little less obvious and less dependent on the system)

Of course, you can also use the while loop to implement it, but the while loop is relatively slower than the for loop.

Manual iteration: iter and next

To support manual iteration of code, python3 also provides a built-in function next, which automatically uses the _ next _ method of an object. Given an iteratable Object z, calling next (z) is equivalent to z. _ next _ (), but the former is much simpler. For example:

From the technical point of view, when the for loop starts, it will be used to provide built-in functions for iter, and an iterator will be obtained from the iteratable object as soon as possible, the returned object contains the required next method.

List and many other built-in objects are not their own iterators because they support multiple open iterators. For such an object, we must call iter to start iteration:

Technically speaking, for loop calls are equivalent to I. _ next __, rather than the next (I)

Now we will show the equality between automatic and manual iterations:

For more information about how to run an action in a try statement and capture exceptions during running, I will post it in detail.

 

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.