And Python iterables iterator object (i.e., the iterator)

Iterable objects and iterators doubts:

Objects may be iterative: may be common for a number of objects are data type of loop iterations may be iterative, such as lists, tuples, dictionaries, set, string generator, range function generates the number of columns and the like, in a wide sense
on, these objects have a built-in iter method, and the method may return an iterator object, when calling the object with iter (iterables), returns an iterator object (iterator class)

Principle for statement is the first acquisition by the iterator iter function iteration object and then calls the next function, which automatically call the next method iterator object, each iteration returns the corresponding value, if no return value, it will StopIter statement throws an exception for automatic catching and processing

Iterator:

Analog statement for the underlying principle:

Write two classes are rewriting method iter and next methods

# Iterator object class

class MyRangeIterator(object):
    def __init__(self, start, end):
        self.index = start
        self.end = end

    def __next__(self):
        if self.index < self.end:
            temp = self.index
            self.index += 1
            return temp
        else:
            raise StopIteration()

# Iterables class

class MyRangeIterable(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end

    def __iter__(self):
        # 该方法返回迭代器对象
        return MyRangeIterator(self.start, self.end)

# 1 used directly for traversing iterables:

for i in MyRangeIterable(1, 10):
    print(i)

Principle # 2.for the bottom, for which in the end things done:
# first step, iter function gets the iterator object:

ret_iterator = iter(MyRangeIterable(1,10))
while True:
    try:
        x = next(ret_iterator) #或者ret_iterator.__next__,实际iter函数和next函数都会反射去执行对象的__next__和__iter__方法,道理一样
        print(x)
    #如果迭代器没有返回值了就抛出异常,退出死循环
    except StopIteration:
        break

Sometimes, we will write the next method iter method and a class, then class creates an object with two identities, both iterables, but also the iterator object, and implemented separately from the above there is a slight difference, such as when the file to read and write, we open a file created objects belong iterable, also belong to the iterator object, you can view by dir, it has both iter method, there are next method, and lists, tuples is a little bit different they are iterables, you can view it iter method dir, but there is no next method, so it was not an iterator, call the object through the iter function, and then return the object is iterable

Iter achieved and the next method in a class, the above MyTRangeIterator next following code MyRangeIterable moved in MyRangeIterable it returns itself

print('----第二种实现迭代器的类---')
class MyRangeIterable(object):
    def __init__(self, start, end):
        # self.start = start
        self.end = end
        self.index = start

    def __iter__(self):
        # 返回对象本身作为迭代器对象
        return self

    def __next__(self):
        if self.index < self.end:
            temp = self.index
            self.index += 1
            return temp
        else:
            raise StopIteration()

my_range = MyRangeIterable(1,20)
for i in my_range:
    print(i)

These are implemented in a class iterator, it is both an iterative object is an iterator object, but there is a drawback, this class creates objects can only be traversed out of the round, because the value of the variable in the final index has in the end, you then traverse the object is no value; but realized separately iterators do not have this problem, because every time initialized.

Guess you like

Origin www.linuxidc.com/Linux/2019-11/161424.htm