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

How to implement external iterator with Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to implement external iterators in Python". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Effect.

Iterators simplify the interface for aggregation

In general, the differences implemented within the aggregation class more or less affect the interface of the aggregation class, as does the interface that accesses its child elements. Due to the limitations of the designer's experience and ability, it is often necessary to add or modify interfaces. After using the iterator pattern, the aggregate class only needs to provide an interface to get the iterator, and the next operation to access the child elements of the aggregate class can be done through the iterator without the need to prepare other interfaces to access the child elements. In addition, because the iterator interface has been tested for a long time and has been proved to meet the needs of most situations, what do you mean, there is basically no need for aggregators to add other access methods.

Support for multiple traversal operations to occur simultaneously on an aggregation

The iterator itself can save the state of the iteration, and a natural result is that multiple iterations can be performed on a cluster at the same time. This is useful when performing certain operations, such as sorting.

Support traversing aggregates in different ways

Because the implementation of the iterator is independent of the aggregate class, you can traverse in different ways by replacing the iterator.

Realize

External iteration or internal iteration

The external iteration refers to the action that the user controls the iteration, while the internal iteration refers to the iterative action automatically performed by the iterator when the user submits the operation to the iterator. The first is the external iterator:

Iterator it = Aggregate.CreateInterator ()

While (! it.isDone ()

{

It.CurrentItem ()-> dosomething ()

It.next ()

}

The action of the iteration, that is, the call to next, is controlled by the user.

Iterator it = Aggregate.CreateInterator ()

While (! it.isDone ()

{

It.CurrentItem ()-> dosomething ()

}

The internal iterator, on the other hand, automatically iterates when the user submits an action to the iterator. This way is very convenient, but the other side of convenience is that it is not flexible enough, not to mention the limited benefits of this convenience. So in most cases, just use an external iterator.

Who will implement the iterative action?

Here are a few options. One is implemented by an iterator, which requires the iterator to understand the internal structure of the aggregate class, which destroys the encapsulation to some extent. Another option is for the aggregate class to implement the iterative action, while the iterator is only responsible for saving the current position of the iteration, which is also called a cursor. There is also a problem with this approach, that is, it is not flexible enough, and adding or adjusting iterative algorithms requires modifications to the aggregation class. In actual development, a third approach can also be used: the aggregation class provides basic iterative functionality, and then additional functionality is provided by another iterator. These functions are usually the filtering of child elements.

Whether an empty iterator is required

The IsDone method of an empty iterator always returns true. Its purpose is to return when the aggregation class does not have a subordinate node, so that code that uses an iterator can use the same logic to process the subordinate node without having to determine whether the aggregate class returns an iterator. This makes the code look more concise.

Do you need a robust iterator

Currently, most iterators do not support adding and deleting elements while iterating. If the programmer does this, the general result is an error. It is very difficult to implement this so-called robust iterator, so the usual practice is to divide the process into two steps: first iteratively retrieve the operands, and then do something else. This approach is easier to implement.

That's all for "how Python implements external iterators". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report