Blog Address: http://www.cnblogs.com/yudanqu/
1, can iterate objects
- Before we introduce iterators, let's talk about an iterative object (iterable), which is an object that can act directly on a for loop.
- So what can be used directly for a for loop?
- There are generally two types of data that can be used directly for a for:
1. Collection data types, such as list, tuple, Dict, set, String ...
2. Generator, including generator and generator function with yield
- So how can we tell if an object is an iterative object? Answer: Isinstance ()
*************************************************************************************************************** *********************
Description: The Isinstance () function to determine whether an object is a known type
Syntax:isinstance(object, classinfo)
parameters: (1)Object--instance Object
(2) ClassInfo--can be a direct or indirect class name, a primitive type, or a tuple composed of them
Return value: Returns True if the object type is the same as the type of ClassInfo, otherwise returns false
*************************************************************************************************************** *********************
Then I'll show you a little chestnut:
In this case, first I import the iterable package, that is, the iterator object, the following four statements are to see the list, tuple, dictionary, string type, you can see that they are four are iterative objects, of course, can iterate over the object far more than these, here just give you a concept, And take a look at how isinstance is used. If you don't understand it, think about whether you've written a sentence (for i in [1,2,3,4,5]), which is a loop representation, which is the element in the loop list that implements the inner statement block. Can also be said to traverse this list, of course, it is iterative.
2. iterators
An iterator can be used not only for a for loop, but also to be called by Next () and return the next value, knowing that a wrong stopiteration error is thrown, indicating that the next value cannot continue to be returned.
Contrast:
Can be called by the next () function and constantly return to the next worthy object called an iterator (Iterator) can be directly acting on a for loop object called an iterative object (iterable)
Here is another little chestnut:
The isinstance inside is still the judging type, and as we can see, (X for X in range (5)) is an iterator, we can take this iterator with a property to receive, so it is convenient for us to call. Note that the iterator can be called by next (), so let's next (L) iterate over the value in the output.
Now that the return value is false, you should be aware of his reasons, [] () {} "" and so on, these are only iterative objects, not iterators. (Note that the two words are not the same)
It is also important to note that other data types can also be converted to iterators:
# Example: List-to-iterator: a = iter ([1,2,3,4])print(Next (a))
Tuples, strings, dictionaries are all possible.
* Below with a small case to let everyone see his usage
' End ' "for in iter" (INPUT,ENDSTR): '\ n' print(str)# is designed so that input does not exit directly, it can be wrapped to continue typing, and can be printed at the same time when you print str
Look at the effect:
The iterator probably introduced here first, welcome everybody and me to study together.
Yu Tanku (yudanqu)
Blog Address: http://www.cnblogs.com/yudanqu/
python--iterators (Iterator)