Python function five (iterator, generator)

Source: Internet
Author: User
Tags generator iterable

One. Iterators

1. Iterative objects (data containing only the __iter__ method is an iterative object)

Common iterative objects: str list tuple dict set range

1.1 What is an iterative object?

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

' Alex '  = [1,2,3,4,5]print('__iter__' in dir (s))  Print('__iter__' in dir (l))# Output Result: # True # True

2. Iterator (the data containing the __iter__ method and the __next__ method is an iterator)

Common iterators: File handles

2.1 The value of the iterator

' Alex '  = [1,2,3,4,5= S.__iter__()print(S1.  __next__())print(S1.  __next__())print(S1.  __next__())#  output Result: a   l   e

The meaning of the 2.2 iterator:
1) The iterator saves memory.
2) iterator inertia mechanism.
3) iterators cannot be repeated and executed down.

The mechanism of the For loop is the perfect use of iterators
Internal contains the __iter__ method, which converts an iterative object into an iterator first. Then in the call __next__ method, he has the exception handling method.

3. The relationship between an iterative object and an iterator

1) Iterative object------> Iterators
An iterative object. __iter__ ()-------> iterators

' Alex '  = [1,2,3,4,5= L.__iter__()     # iterator print(L1)   #  iterator  follows iterator protocol #  output: <list_iterator object at 0x00000156b0ec7630> 

To determine whether an iterator is an object or an iterator

Method Two (Isinstance is similar to type but more powerful than type )

L = [1,2,3,4,5]l_iter= L.__iter__() fromCollectionsImportiterable fromCollectionsImportIteratorPrint(Isinstance (l,iterable))#TruePrint(Isinstance (L,iterator))#TruePrint(Isinstance (L_iter,iterator))#FalsePrint(Isinstance (L,list))#True
Two. Generator

The essence of the generator is the iterator, which is the iterator that the generator writes with its Python code.
1, you can use the generator function
2, an iterator can be built with various derivations.
3, can be converted by data.

defgener ():Print(111)    yield222Count=yield333Print(count)yield444yield555g=Gener ()Print(g)Print(g.__next__())Print(g.__next__())Print(G.send ('AAA'))Print(g.__next__())#Output Result:#<generator Object Gener at 0x0000014cde5b1bf8>#111#222#333#AAA#444#555

4.send
1) Send and next functions as
2) Send a value to the previous yiled overall
Send value cannot be sent to last yield
When you get the first value, you cannot use send only with Next

    

Python function five (iterator, generator)

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.