To simply say the generator and the iterative object in Python and the problem of iterators. Simply documenting it doesn't involve much in-depth content.
First of all, what is a generator, first look at the following code:
1 # _*_ Coding:utf-8 _*_ 2 3 result = (x for x in range (10 4 5 " 6 print results under Print 7 <generator object <genexpr> at 0x0000026fa092b360> 8 "" 9 print (Result)
The code above is actually a generator, and through print it is found that we cannot print out the computed results.
If you want to calculate the result, you can only do it in two ways, the next () method and the For loop. Look at the following code:
1result = (x forXinchRange (10))2 3 Print(Next (Result))#04 Print(Next (Result))#15 .6 .7 .8 Print(Next (Result))#99 Ten #a stopiteration error will occur long after the values in the generator have all been removed One Print(Next (Result))#stopiteration A - - Print("===============================") the - #The simplest way to take out the generator values is to use a for loop, while avoiding exceptions - forXinchResult: - Print(x)
The following is a bit of an iterative object:
Simply put, an object that can act directly on a for loop can be called an iterative object (iterable).
For example, the generator we said above is an iterative object.
In many commonly used data types, such as: List,dict,tuple,set,str and generator, these are iterative objects.
So how do you tell if a data type is an iterative object? Can be judged using isinstance (), here is a simple example code:
1 # Coding:utf-8 2 from Collections Span style= "COLOR: #0000ff" >import *3 4 if isinstance ([],iterable): 5 print (" Span style= "COLOR: #800000" >yes " ) 6 else : 7 print ( " no ")
Do not forget to introduce the corresponding module when using the above code.
Again, the iterator:
The iterator is simply that it can be called continuously by the next () method, and the object that returns the next value is an iterator, Iterator.
(for example, the generator that was first mentioned is an iterator, and the generator is also an iterative object.) It is important to note that all generators are iterators, and all iterators are not necessarily generators.
Again, we can use Isintance () to determine whether a data is an iterator or not.
The code that is judged here is not written, as is the case with the above judgment code.
In fact, after practice, it will be found that many of the commonly used data types are iterative objects (iterable) but not iterators (Iterator), so this time, if the development needs, we can use the ITER () method to convert an iterative object into an iterator.
1 #Coding:utf-82 fromCollectionsImport*3 #Create a list4List_test = [A]5 #Determine if this list is an iterator6 ifisinstance (list_test,iterator):7 Print("Yes")8 Else:9 Print("No")#output result is noTen One #convert using ITER Aresult =iter (list_test) - #You can use the next () method after the conversion is complete - Print(Next (result)) the - #You can also use the For loop - forIinchResult: - Print(i) +
Here's a quick summary:
There are three main concepts: generator, iterative object, iterator
The generator can take the value from the next method, or it can get the value through a for loop
An iterative object that can be used for a For loop is an iterative object, and the generator is also an iterative object
An iterator that, in addition to being an iterative object, is an iterator. You can use the Isinstance () method to determine whether an object is an iterator or an iterative object, and basically many of the data types we use are iterative objects, but not iterators, but we can use the ITER () method to convert an object to an iterator.
Python's generators and iterators and iterative objects