Python Beginner's Handbook of Learning----Loop objects

Source: Internet
Author: User

What is a Loop object loop object is an object that contains a next () method (__next__ () method, in Python 3x), which is intended to proceed to the next result, and to cite a stopiteration error after ending a series of results.
When a loop structure (for example, for) invokes a loop object, it invokes the next () method every time it loops, until Stopiteration appears, and the For loop receives it, knowing that the loop has ended and stops calling next ().


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

Let's run the python command line:

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

>>>f.__next__ ()

>>>f.__next__ ()
...

Continuously enter F.next () until the last occurrence stopiteration
open () is actually a loop object that returns, including the __next__ () method. The __next__ () method returns the contents of a new line each time, and stopiteration is given when the end of the file is reached. In this way, we are the equivalent of a manual loop.
If you do this automatically, it is:
For on open (' Test.txt '):    print Line
Here, the for struct automatically calls the next () method, assigning the return value of the method to line. The loop knows when the stopiteration comes to an end.
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 a middle layer between the loop object and the For loop call, which is to convert the Loop object toiterators (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.
GeneratorGenerator (Generator)The main purpose is to form a user-defined loop object.


Generator'swriting methods and function definitions are similar, but instead of the return placeyield。 The generator can havemultiple yield。 When the generatorWhen a yield is encountered, the generator is paused,returnThe 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:
Def gen ():    a =    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
Output results
1008001000

Consider one of the following generators:
Def Gen (): for    I in range (4):        yield I
It can also be writtenGenerator Expressions (Generator expression):

G = (x for x in range (4))
Table Deduction table derivation (list comprehension) is a quick way to generate a list. Its syntax is simple, it has practical value.
Suppose we generate the list L:
L = []for x in range:    l.append (x**2)
The above produces the table L, but in fact there is a quick way of writing, that is, table deduction method:
L = [x**2 for x in range]     #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Previous: Python rookie study manual one by one----exception handling next: If you have any questions welcome to my public question ~

Python Beginner's Handbook of Learning----Loop 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.