Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the understanding of the Python loop object

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the understanding of the Python loop object? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Circular object does not exist with the birth of Python, but it develops rapidly, especially in the era of Python 3x, circular object is becoming the standard form of loop.

What is a circular object?

A circular object is an object that contains a next () method (_ _ next__ () method, in python 3x), which is intended to proceed to the next result and enumerates a StopIteration error at the end of a series of results.

When a loop structure (such as for) calls the loop object, it calls the next () method every time the loop occurs, until the StopIteration appears and the for loop receives it, it knows that the loop is over and stops calling next ().

Suppose we have a file for test.txt:

1234abcdefg

Let's run the python command line:

> f = open ('test.txt')

> > f.next ()

> > f.next ()

...

Keep typing f.next () until StopIteration finally appears

What open () returns is actually a circular object that contains the next () method. The next () method returns the contents of a new line each time, and enumerates StopIteration when it reaches the end of the file. In this way, we are equivalent to a manual loop.

If it is carried out automatically, it is:

For line in open ('test.txt'): print line

Here, the for structure automatically calls the next () method, assigning the return value of that method to line. The loop knows that it ends when a StopIteration occurs.

As opposed to sequences, the advantage of using loop objects is that you don't have to generate elements to use before the loop begins. The elements used can be generated one at a time during the loop. In this way, the space is saved, the efficiency is improved, and the programming is more flexible.

Iterator

Technically, there is an intermediate layer between the circular object and the for circular call, which is to convert the circular object into an iterator. This conversion is achieved by using the iter () function. But logically, this layer can often be ignored, so loop objects and iterators often refer to each other.

Generator

The main purpose of the generator is to form a user-defined loop object.

The generator is written in a way similar to the function definition, except that it is changed to yield in the case of return. There can be multiple yield in the generator. When the generator encounters a yield, it pauses to run the generator and returns the value after the yield. When the generator is called again, it continues to run from where it was paused until the next yield. The generator itself forms a loop, using one value returned by yield at a time.

Here is a generator:

Def gen (): a = 100yield an a = axi8 yield a yield 1000

The generator has three yield, and if used as a circulator, it loops three times.

For i in gen (): print I

Consider the following generator:

Def gen (): for i in range (4): yield I

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

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

Generator expressions are an easy way to write generators. Readers can refer to it further.

Table derivation

Table derivation (list comprehension) is a way to generate tables quickly. Its grammar is simple and has practical value.

Suppose we generate table L:

L = [] for x in range (10): L.append (xylene 2)

The above produces table L, but there is actually a quick way to write it, that is, the way of table derivation:

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

This is similar to a generator expression, except that it uses square brackets.

(the mechanism of table derivation is actually the use of loop objects, which you can refer to if you are interested. )

What will be generated by practicing the following table derivation?

Xl = [1Mage3Pol 5] yl = [9JEI 12je 13] L = [xmemt 2 for (XRecine y) in zip (xl,yl) if y > 10] this is the answer to the question about how to understand the object of the Python loop. I hope the above content can be of some help to you. If you still have a lot of doubts unsolved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report