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 raised at the end of a series of results.
In the For loop, Python automatically invokes the factory function iter () to get the iterator, automatically call next () to get the element, and also completes the work of checking stopiteration exceptions.
Several commonly used built-in data structures tuple, list, set, dict all support iterators, strings can also use the iterative operation.
You can also implement an iterator yourself, as described above, by simply returning an object in the __iter__ method of the class, which has a next () method that throws the stopiteration exception at the right time. But there are not many times when you need to implement iterators yourself, and it's easier to use a builder even if you need to.
Copy Code code 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]
An obvious benefit of using iterators is that each time you read only one piece of data from an object, it does not incur excessive memory overhead.
For example:
Copy Code code as follows:
/* Load the file into memory one at a time, then print line by row. 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 run the writing, he did not explicitly read the file, but the use of iterators each read off a line * *
For line in open ("Test.txt"): #use file iterators
Print Line
Build device
The generator is written in a similar way as the function definition, except to change the return location to yield.
There can be multiple yield in the builder. When the generator encounters a yield, it pauses running the generator, returning the value after yield. When you call the generator again, it will continue to run from where it was paused until the next yield.
The generator itself forms an iterator, using a value returned by a yield each iteration.
It should be noted that the return statement is not required in the builder, and the returned value is not specified, and the default return statement already exists in the generator
Builder expression
Copy Code code as follows:
(I for I in range (5))
Return iterator
<generator Object <genexpr> at 0x7ff3e8f0d960>
List resolution, returning to lists
Copy Code code as follows:
[I for I in range (5)]
Back to List
[0, 1, 2, 3, 4]
There is a problem here, that is, range (5) returns a length of 5, and if range (1000) takes up a 1000-size array space, and if we use the ' generator ' to generate a number when needed, the footprint of the space is reduced, Here we can use the xrange () function to implement.
Copy Code code as follows:
'''
Xrange
Function Description: The usage is exactly the same as range, but instead of generating an array, it is a generator.
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 is better than range, especially when the return is very large, try to use Xrange bar, unless you are going to return a list.