Python iterators and iterator objects

Iterables

① iterables defined

For iterator, we should be more familiar iterable, before either the source or the lecture in more or less we mentioned iterables word. In order to facilitate better understanding before iterables, it may explain the not quite right, so today we have a formal chat What is iterable. From the literal meaning, we are dismantling them first: What is an object? Everything in Python objects, a variable we have said before, a list, a string, a file handle, function name, and so can be called an object, in fact, an object is a case in point is a real thing. So what is the iteration? In fact, we often encounter in their daily lives iteration of this word, update iteration and so on, iteration is an iterative process, but not a simple repetition (if simply repeating that he is no different cycle) each repetition It is based on the results from the last time. For example, your father angry with you, you were born your father, oh no, you were born your son, your son, your grandson born and so on, each generation is not the same; and you use a nice app, WeChat vibrato and so on, every based on the last period of time will do some updating, then this is iterative. Iterables from the literal meaning, it is a repeat value of the real thing.

So we just are analyzed from the literal meaning of what is iterable, so far we are exposed to iterables, what does?

str list tuple dic set range of file handles, etc., then int, bool Why these can not be called iterables it? While watching these do not meet the literal meaning, but we have a certain criteria or rules to judge the object is not iterable.

In python, whenever an object contained inside __iter__ methods are iterable.

② view the object internal methods

The internal object contains the source code to see what methods in addition to what other way to solve it? With, of course, by dir () to judge what object has a method

s1 = 'miracle'
print(dir(s1))

dir () returns a list, which contains all the object as a string method name. In this way we can determine an object in python is not the iterable:

s1 = 'miracle'
i = 100
print('__iter__' in dir(i))  # False
print('__iter__' in dir(s1))  # True

③ summary

Literally speaking: iterables is a repeat value of the real thing.

From a professional standpoint: internal objects whenever containing __iter__ methods are iterable.

Iterables can determine whether the object has ' ITER judged' method.

Can advantage iteration object:

You can visually see the data inside.

Iteration can drawback object:

  1. used internal memory.

  2. Iterables not iterative values ​​(other than the index, key removed).

So some people questioned this shortcoming, and even blows index, other than key, which I can value it for loop! Yes, they can be carried out by the value for the cycle, in fact, for circulation at the bottom made a little transformation, is the first iteration of the object can be converted into an iterator, then carrying value of. So then, we take a look at the iterator is what the hell.

Iterator

① defined iterator

Literally speaking iterator can iterate values ​​is a tool that is: as a tool more appropriate here.

From a professional standpoint: iterator object is: a method implements __next__ no arguments, returns the next element in the sequence, if no element, then the iterator StopIteration exception thrown is also achieved .python __iter__ method, and therefore may be an iterative iterators. From "smooth python"

So there are a number of explanations for the above advance, and difficult to understand, do not be too tangled, our simple terms: the python, the interior includes ' Iter ' methods and contain ' the Next target' approach is iterator.

② how to determine whether the object is an iterator

ok, so we have this definition, we can determine the number of objects is not an iterator or a iterables, please judge these objects: str list tuple dict set range of file handles which is an iterator, which is iterables:

o1 = 'miracle'
o2 = [1, 2, 3]
o3 = (1, 2, 3)
o4 = {'name': 'cyx','age': 18}
o5 = {1, 2, 3}
f = open('file',encoding='utf-8', mode='w')
print('__iter__' in dir(o1))  # True
print('__iter__' in dir(o2))  # True
print('__iter__' in dir(o3))  # True
print('__iter__' in dir(o4))  # True
print('__iter__' in dir(o5))  # True
print('__iter__' in dir(f))  # True
# hsagn
print('__next__' in dir(o1))  # False
print('__next__' in dir(o2))  # False
print('__next__' in dir(o3))  # False
print('__next__' in dir(o4))  # False
print('__next__' in dir(o5))  # False
print('__next__' in dir(f))  # True
f.close()

Can verify the code above, these objects before we learned, only the file handle is an iterator, the rest of those data types are iterable.

How ③ iterables converted into iterator

l1 = [1, 2, 3, 4, 5, 6]
obj = l1.__iter__() 
# 或者 iter(l1)
# print(obj) 
# <list_iterator object at 0x000002057FE1A3C8>

④ iterator value

Iterables been iterative value is not (removal index, slicing and Key), but can be converted to the iterator, using the value of the iterator is __next __ ():

l1 = [1, 2, 3,]
obj = l1.__iter__()  # 或者 iter(l1)
# print(obj)  # <list_iterator object at 0x000002057FE1A3C8>
ret = obj.__next__()
print(ret)
ret = obj.__next__()
print(ret)
ret = obj.__next__()
print(ret)
ret = obj.__next__()  # StopIteration
print(ret)
# 迭代器利用next取值:一个next取对应的一个值,如果迭代器里面的值取完了,还要next,
# 那么就报StopIteration的错误。

Inner loop simulation for the mechanism ⑤while

We have just mentioned, the circulation loop must be a target for iterable, but that does not mean you can iterables value, because the internal mechanism for loop is: convert iterables into an iterator, then use the next be value. Finally, exception handling process StopIteration thrown.

l1 = [1, 2, 3, 4, 5, 6]
# 1 将可迭代对象转化成迭代器
obj = iter(l1)
# 2,利用while循环,next进行取值
while 1:
    # 3,利用异常处理终止循环
    try:
        print(next(obj))
    except StopIteration:
        break

⑥ Summary

Literally speaking: iterator that can iterate the value of the tool.

From a professional standpoint: in python, containing internal ' Iter ' method and containing ' Next objects' methods iterators.

Iterator advantages:

  1. Saving memory:
    iterator equivalent to only one data space in memory: because both the value of each piece of data in memory will be released this entry load current data.
  2. Inert mechanisms:
    the Next time, take a value, not too much value.

Iterator has a good pattern can explain the two above: iteration is the cornerstone of data processing. When the scan does not fit in memory data sets, we need to find a way to get the data item inert, that demand fetch one data item. This is an iterative mode.

Iterator disadvantages:

  1. Not intuitive view of data inside.

  2. When the value is not turning back, only it has value down.

l1 = [1, 2, 3, 4, 5, 6]
obj = iter(l1)

for i in range(2):
    print(next(obj))

for i in range(2):
    print(next(obj))

Iterative iterator object may be comparative

Today more in-depth understanding of the iterable iterator, then we talk about and compare between the two applications:

  1. Iterables:
    is a proprietary method more flexible operation (such as a list, CRUD dictionaries, and the like commonly used method of operation of the string), more intuitive, but take up memory, and not directly through the value of such a loop iteration a data set.

    Application: When you focus on flexibility for data processing, and sufficient memory space, the data set is set to iterables is the clear choice.

  2. Iterator:

    Is a very save memory, can record the position values, the values ​​may be directly through the loop + next method, but not intuitive method of operating a single set of data comparison.

    Application: When your data is too large, large enough to explode your memory or your memory as to save the first factor, the data set is set iterator is a good choice.

Reprinted from: https://www.cnblogs.com/jin-xin/articles/10854130.html

Published 12 original articles · won praise 7 · views 159

Guess you like

Origin blog.csdn.net/caiyongxin_001/article/details/105036451