Python in iterable (iterable) vs iterator (iterable)

A .iterable (iterable)

Official document translation:

iterable is able to return once the object of one of its members. Iterables include all sequence types (such as list, str, tuple), some non-sequence data types (e.g., dict, file objects), all defined __iter class object __ () method, all defined _ Serialization semantics _getitem __ () method of the class object.

Iterables can be used for cycles and many other places (zip (), map (), ...) required sequence. When iterables be passed as a parameter to the function built-ITER () call, iter () returns an iterator object. Iterator is adapted to pass a first set of values. When iterables, usually do not need to call iter () method or process iterator object, because the for statement will automatically do this for you - it will create a temporary unnamed variable to hold for you during the cycle iterators. Reference: iterator, sequence, and generator. 

Two .iterator (iterators)

Official document translation:

iterator is an object representation of the data flow. Repeat the __next __ call the iterator () method (or the iterator is transmitted to the built-in function Next ()) returns a continuous stream of data items. When there is no data can be accessed, it will lead to a StopIteration exception. This time, the iterator object is exhausted, any further follow-up call to __next __ () method will again lead StopIteration are an exception.

Iterator needs to have a __iter __ () method, __ iter __ () method returns the iterator object itself, so an iterator is any iterable, and can be used in most receiving iterables place. Outside a row is worth noting that a number of iterations to try the code passed.

When the time a container object (e.g., list) is transmitted to the ITER () method, or for use in the loop, generates a new iterator object - - using the iterator will try this operation returns to the last iteration depleted same iterator object used, such as iterators up an empty container.

Three .iter ()

May be next () function call and return to the next target value continuously referred iterator: Iterator.

Generator Iterator objects are, but the list, dict, str though Iterable, not the Iterator.

The list, dict, str becomes like Iterable Iterator may be used ITER () function:

1 from  collections import Iterable
2 a=[1,2,4,6,8,6,10]
3 a=iter(a)
4 print(next(a))
5 print(next(a))
6 print(next(a))

 

Guess you like

Origin www.cnblogs.com/neseker/p/11911043.html