"Python" Learning note 15: Looping objects

Source: Internet
Author: User

Looping objects

The so-called loop object, which contains a next () method (__next__ () in Python3), is intended to proceed to the next result, and after the end of a series of results, the stopiteration error is enumerated.

When a loop structure (for example, for) invokes a loop object, it invokes the next () method every time it loops, until the stopiteration appears, and the For loop receives it, knowing that the loop has ended and stops calling next ()

Suppose we have a file data.txt

My 中文版 is not good. My 中文版 isn ' t very well. I can speak a little 中文版.

Run the python command line

>>>f = open (' Data.txt ')

>>>f.__next__ ()
' My 中文版 is not good.\n '
>>>f.__next__ ()
"My 中文版 isn ' t very well.\n"
>>>f.__next__ ()
' I can speak a little 中文版. '
>>>f.__next__ ()
Traceback (most recent):
File "<input>", line 1, in <module>
Stopiteration

The open () return is actually a looping object that contains the __next__ () method, and the next () method returns the contents of a new row each time, and stopiteration when the end of the file is reached. In this way, we are the equivalent of a manual loop.

The automatic loop is as follows:

Data Open ("data.txt"):    print(data)
Here, the for struct automatically calls the next () method, assigning the return value of the method to line. Loop knows when the stopiteration is present end generator

The main purpose of the generator (generator) is to make up a user-defined loop object.

The generator is written in a similar way to the function definition, except that it is changed to yield in the return place. There can be more than one yield in the generator. When the generator encounters a yield, it pauses to run the generator, returning the value after yield. When the generator is called again, it will continue to run from where it was just paused until the next yield. The generator itself forms a circulator that uses a yield return value each time it is cycled.

Here is a generator example:

def Generator ():    a =    yield a    a = a*10    yield a    yield 10000
The generator has a total of three yield and three loops when used as a circulator.

For I in Generator ():    print(i)

Printing results:

100
1000
10000

A second generator example:

def Gen (): for    I in range (6):        yield I

Call Builder

For J in Gen ():    print(j)

Print results

0
1
2
3
4
5

Table derivation

Table derivation (list comprehension) is a quick way to generate tables. Its syntax is simple, it has practical value.

Table Deduction Case:

L = [x**2 for x in range (4)]

Print

For i in L:    print(i)

Print results

0
1
4
9


"Python" Learning note 15: Looping objects

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.