Python iterators and generators

Source: Internet
Author: User
Tags iterable

One, iterator

an iterative object is now known: str list tuple dict set range and file handle.

But what is an iterative object? The method of measurement for an iterative object.

Method One: Dir (the object being measured) if he contains __iter__, then this object is called an iterative object.

The following string s is an iterative object:

s= ' abcdef ' Print (' __iter__ ' in Dir (s))
can iterate over objects, what does iterable have to do with iterators? An iterative object can be converted to an iterator ( an iterative object. __iter__ ()).
S1 = s.__iter__ () print (S1) #iterator  follows the iterator protocol
The value of the iterator: (iterator. __next__ ())
#迭代器取值: S2 = ' ABCD ' s3 = s2.__iter__ () print (s3.__next__ ())   #aprint (s3.__next__ ())   #b
Data that contains only the __iter__ method is an iterative object that contains the __iter__ method, and the data that contains the __next__ method is an iterator
#迭代器的判断: The data that has the __iter__ method and contains the __next__ method is the iterator L = [1,2,3,4]print (' __iter__ ' in Dir (l))  #Trueprint (' __next__ ' in Dir (l ))  #Falseliter = l.__iter__ () print (' __next__ ' in Dir (Liter))  #Trueprint (liter.__next__ ())     #1print ( Liter.__next__ ())     #2print (liter.__next__ ())     #3print (liter.__next__ ())     #4

 Method Two: Measure whether he is an iterator or an iterator:

# method Two Tests whether he is an iterator or an iterator object L = [1,2,3]l_iter = l.__iter__ () from collections import Iterablefrom collections Import Iteratorprint ( Isinstance (l,iterable))      #Trueprint (Isinstance (l,iterator))      #Falseprint (Isinstance (l_iter,iterator)) # Trueprint (Isinstance (l,list))          #Truedied  

 The meaning of iterators:

1), iterators save memory.
2), the iterator inertia mechanism.
3), iterators can not be repeated, always down execution.

Internal mechanism for the For loop:

1), internal contains the __iter__ method, he will be iterative objects first into an iterator.
2), and then call the __next__ method.
3), the For loop has an exception handling method. 

#用迭代器来表示for循环的内部机制for I in [N/a]:    print (i) L = [1,2,3]l_iter = l.__iter__ () while True:    try:        print (L_iter . __next__ ())    except stopiteration:        break

 Second, generator

 

 

Python iterators and generators

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.