Introduction to Python iterators and generators

Source: Internet
Author: User
iterators

An iterator is an object that implements an iterator protocol, and the iterator protocol in Python is that an object with the next method advances to the next result, and Stopiteration is thrown at the end of a series of results.

In the For loop, Python automatically calls the Factory function iter () to get the iterator, automatically calls next () to get the element, and completes the work of checking the stopiteration exception.

Several commonly used built-in data structures tuple, list, set, dict all support iterators, and strings can also use iterative operations.

You can also implement an iterator yourself, as described above, by simply returning an object in the class's __iter__ method, which has a next () method that throws the stopiteration exception at the appropriate time. But there are few times when you need to implement an iterator yourself, and even if you need it, it's easier to use the generator.

The code is as follows:


#!/usr/bin/env python
# Coding=utf-8

Class Test:
def __init__ (self, input_list):
Self.list = Input_list
SELF.I = 0

def __iter__ (self):
return self

Def next (self):
if self.i = = Len (self.list):
SELF.I = 0
Raise Stopiteration
SELF.I + = 1
return Self.list[self.i-1]

One of the obvious benefits of using iterators is that you only read one piece of data from the object at a time, without causing too much memory overhead.

For example:

The code is as follows:


/* Load the file into memory one at a time and print it on a line-by-row basis. When the file is large, the memory overhead of this method is very large.
For line in open ("Test.txt"). ReadLines ():
Print Line

/* This is the simplest and fastest to use, he does not explicitly read the file, but instead uses the iterator to read one line at a time */
For line in open ("Test.txt"): #use file iterators
Print Line

Generator

The generator is written in a similar way to the function definition, except that it is changed to yield in the return place.

There can be more than one yield in the generator. When the generator encounters a yield, it pauses to run the generator, returning the value after yield. When the generator is called again, it will continue to run from where it was just paused until the next yield.

The generator itself forms an iterator that uses the value returned by a yield at each iteration.

It is important to note that the return statement is not required in the generator, there is no need to specify a return value, and the default returns statement already exists in the generator

Builder expression

The code is as follows:


(I for I in range (5))
Return iterator
At 0x7ff3e8f0d960>


List parsing, returning to the list

The code is as follows:


[I for I in range (5)]
Back to List
[0, 1, 2, 3, 4]

One problem here is that range (5) returns a 5-length data, and if it is a range (1000) it takes up a 1000-size array space, and if we use the ' generator ' to generate a number when needed, the space occupancy will be reduced. Here we can use the xrange () function to implement.

The code is as follows:


'''
Xrange
Function Description: The usage is exactly the same as range, the difference is not an array, but a builder.
xrange Example:
'''
>>> xrange (5)
Xrange (5)
>>> List (xrange (5))
[0, 1, 2, 3, 4]
>>> xrange (1,5)
Xrange (1, 5)
>>> list (xrange (1,5))
[1, 2, 3, 4]
>>> xrange (0,6,2)
Xrange (0, 6, 2)
>>> list (xrange (0,6,2))
[0, 2, 4]

So xrange do loop performance better than range, especially when returning very large, try to use Xrange bar, unless you are going to return a list.

  • 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.