Python advanced 06 Loop Object

Source: Internet
Author: User
Tags function definition

The purpose of this section is to have a basic concept for the loop object when you are reading the Python program.

The Loop object does not exist with the advent of Python, but it is developing rapidly, especially in the Python 3x era, where the cyclic object is called the standard form of the loop.

What is a looping object

The Loop object is an object that contains a next () method (The __next__ () method, in Python 3x), which is intended to proceed to the next result and, after ending a series of results, to cite the stopiteration error.

When a loop structure (for example, for) calls a loop object, he calls the next () method every time the loop is made, until Stopiteration appears, and the For loop receives it, knowing that the loop is over and stops calling next ().

Let's say we have a Test.txt file:

1234

Abcd

Efggds

Let's run the python command line:

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

>>>f.next ()

>>>f.next ()

...

Continuously enter F.next () until the last occurrence stopiteration

The open () return is actually a loop object that contains the next () method. The next () method returns the contents of the new line, and when the end of the file is reached, the stopiteration is quoted, so that we have the equivalent of a manual loop.

If you do this automatically, it is:
For line in open (' Test.txt ')

Print Line

Here, the for struct automatically calls the next () method, assigning the return value of the method to line. Loop until the end of the stopiteration appears.

The advantage of using a loop object relative to a sequence is that you don't have to create the element you want to use when the loop has not started. The elements used can be generated successively during the loop. This saves space, improves efficiency, and makes programming more flexible.

Iterators

Technically, there is also a middle layer between the loop object and the For loop call, which is to convert the loop object to an iterator (iterator). This conversion is accomplished by using the ITER () function. However, this layer can often be ignored on a logical level, so loop objects and iterators often refer to each other.

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 the return is changed to yield. There can be more than one yield in the generator, and when the generator encounters a yield, it pauses the generator and returns 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, and the generator itself forms a circulator, each cycle using a yield return value.

Here is a generator:

Def gen ():

A = 100

Yield a

A = A*8

Yield a

Yield 1000

The generator has a total of three yield and three loops when used as a circulator.

For I in Gen ():

Print I

Consider one of the following generators:

Def gen ():

For I in range (4):

Yield I

It can also be written as a generator expression (Generator expressions)

G = (x for x in range (4))

Builder expressions are a simple way to write a generator. The reader may consult further.

Table pushed to

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

Suppose we generate table L:

L = []

For x in range (10):

L.append (x**2)

The above produced the table L, but in fact, the quick way of writing, that is, table deduction method:

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

This is similar to the builder expression, except that it uses the brackets.

(The mechanism of the table deduction is actually the use of the Loop object, interested can be consulted)

Python advanced 06 Loop Object

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.