python-iterator, generator and for loop mechanism

Source: Internet
Author: User
Tags function definition

One: What is an iterator protocol

1. The iterator protocol means that the object must provide a next method that either returns the next item in the iterator, or causes a Stoplteration exception to terminate the protocol (only backward cannot go forward)

2. An iterative object: An object that implements an iterator protocol (how to: Define an _iter_ () method within an object)

3. The Protocol is a convention that iterates over an object, implements an iterator protocol, and Python's internal tools (such as a For loop, sum,min function, etc.) use an iterator protocol to access an object.

Two: forloop mechanism

The nature of the 1.for loop mechanism: Loop through all objects, all using an iterator protocol.

Three: what is a generator

1. The generator, which can be understood as a data type, automatically implements the iterator protocol (other data types need to call their own built-in _iter_ method), so the generator is an iterative object.

2. Generator classification: There are two representations in Python (Python provides generators in two different ways)

"1" generator function: The general function definition, however, returns the result using the yield statement instead of the return statement, the yield statement returns one result at a time, and in the middle of each result suspends the state of the function so that it can continue execution the next time it leaves.

1 defTest ():2     yield13     yield24     yield35res=Test ()6 Print(RES)7 Print(Res.__next__())8 Print(Res.__next__())9 #Result: <generator Object Test at 0x0000023e254bd8e0>Ten #1 One #2

"2" generator expression: Similar to list derivation, but the generator returns an object that produces results on demand, rather than building a list of results at a time.

1Laomuji= ('Egg%d'%i forIinchRange (10))2 Print(Laomuji)3 Print(Laomuji.__next__())4 Print(Laomuji.__next__())5 Print(Laomuji.__next__())6 Print(Next (Laomuji))7 Print(Next (Laomuji))8 #Result: <generator object <genexpr> at 0x0000015f1262d8e0>9 #Egg 0Ten #Egg 1 One #Egg 2 A #Egg 3 - #Egg 4

3. List Resolution :

1egg_list=[]2  forIinchRange (10):3Egg_list.append ('Egg%d'%i)4 Print(egg_list)5 #results: [' egg 0 ', ' egg 1 ', ' Egg 2 ', ' Egg 3 ', ' Egg 4 ', ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' egg 9 ']6 7 List Resolution:8l=['Egg%d'%i forIinchRange (10)]9 Print(L)Ten #results: [' egg 0 ', ' egg 1 ', ' Egg 2 ', ' Egg 3 ', ' Egg 4 ', ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' egg 9 '] One #list resolution puts the results of the list that will be generated before the for loop is placed behind A  - ternary expression: (three operations, the For loop is preceded by one, for loop is one, if condition this is a) -l1=['Egg%d'%i forIinchRange (10)ifI>5 ] the Print(L1) - #results: [' egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 '] - #no four-dollar expression

Summarize:

1. The [] Conversion of the list parsing [] to () is the generator expression

2. List parsing and builder expressions are a convenient way to become, but generator expressions are more memory-saving

3.Python not only uses the iterator protocol to make the For loop more generic. Most built-in functions also use iterator protocols to access objects. For example, the SUM function is a python built-in function that accesses an object using an iterator protocol, and the generator implements an iterator protocol, so we can directly compute a series of worthwhile and:

1  for  in range (4))

Instead of superfluous, construct a list first:

1  for  in range (4)])

python-iterator, generator and for loop mechanism

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.