1. iterator (itertor):
Iteration: iterative feedback process is repeated activities, its purpose is usually to approach the desired goal or outcome. Each repetition of the process is called an "iteration", and the results will be obtained for each iteration as the initial value for the next iteration.
2. explore iterator works:
Type may be used for loop: list, dic, set, turple, f = open (), enumerate
Why are these types can be used for loop?
print (dir (type)): The method may be used to view the presence of function inside, i.e., those types which are present some of the same methods, and these methods constitute cycle this function
Cycle principle:
Cycle principle, i.e. the function calls the method: using the method of Example calling double-underlined: print ([] .__ iter __ ())
Inquiry is composed of those functions within the cycle this feature?
Intersection determination may be used are those within the function (to set the conversion types, performing an intersection operation)
Example:
ret = set(dir([]))&set(dir({}))&set(dir(''))&set(dir(range(10)))) print(ret)
Output:
{'__len__', '__getattribute__', '__str__', '__getitem__', '__lt__', '__gt__', '__ne__', '__new__', '__dir__', '__subclasshook__', '__delattr__', '__eq__', '__iter__', '__ge__', '__reduce_ex__', '__doc__', '__contains__', '__sizeof__', '__class__', '__init__', '__le__', '__hash__', '__format__', '__repr__', '__setattr__', '__reduce__', '__init_subclass__'}
Determining whether there is the method of some type of inquiry iter whether :( below in list, is the case, the result is True)
print ( " __iter__ " In dir ([]))
Use set difference analysis can be drawn:
print (set (you ([]. __iter__ ())) - set (you ([])))
The results are:
{'__length_hint__', '__setstate__', '__next__'}
Further another method to further explore the function meaning:
Example:
l=[1,2,3] itertor = l.__iter__() print(itertor.__next__()) print(itertor.__next__()) print(itertor.__next__())
Results: 1
2
3
Iterable (available iterations), itertor (iterator)
[] .__ iter __ () is iterative
[] .__ next __ () may be taken one by one from the element iterator
Type and contains next method iter is an iterator
May be available for circulation are iterative
Internal function iterative method must have iter
As long as the iterator it must be iterative
# For only when the time iterables to use for
when we encounter a new variable, we can not determine whether it can forx cycle time, whether it is on the fat segment iteration
for i in L:
Pass
itertor = i .__ iter__ ()
itertor = I .__ Next __ ()
'' '
#
' ''
benefits iterator:
can take the container type in a value of one
to save memory space, takes a value, release a value
if the normal use of print (list (range (10000))) will be a one-time memory for
'' '