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 Python 3 manually creates iterators

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how Python 3 creates iterators manually, which is concise and easy to understand, and will definitely catch your eye. I hope you can learn something from the details of this article.

Today we'll learn how Python 3 creates iterators manually!

Overview

Iterators can traverse sequence objects such as lists, dictionaries and strings, or even custom objects, whose essence is to record the location of each element in the iterative object. The iteration process goes from the first element to the last element, and the process cannot be rolled back or iterated in the opposite direction.

Two basic methods: iter and next

Sequence objects can create iterators directly using iter () and iterate through next ().

Iteration using for cycle

S = 'PYTHON'IT = iter (S) for it in IT: print (it)

Example result:

PYTHON

Using next () iteration

S = 'PYTHON'IT = iter (S) print (next (IT)) print (next (IT))

Example result:

PYTHON

When we use the next () iteration, StopIteration is raised if the number of iterations exceeds the number of elements in the iterator, so we can use the while loop to iterate and constantly catch exceptions at the end of the iteration to complete the iteration process of the for loop.

S = 'PYTHON'IT = iter (S) while True: try: print (next (IT)) except StopIteration: breakPYTHON manually build iterator

Using a class as an iterator requires the implementation of two methods, iter () and next (), in the class. The iter () method returns a special iterator object that implements the next () method and marks the completion of the iteration with a StopIteration exception. The next () method (next () in Python 2) returns the next iterator object. The following class constructs an iterator that accepts an iterated number, returns the square of the previous iteration result for each iteration, and throws an exception when the iteration result is greater than 999999999999. Use this class creation example to square and iterate over the number 2.

Class IT_SQUARE: def _ init__ (self, x): self.x = x def _ next__ (self): self.x = self.x * * 2 if self.x > 999999999999: raise StopIteration else: return self.x def _ iter__ (self): return self IT1 = IT_SQUARE (2) while True: try: print (IT1.__next__ ()) except StopIteration: break

Sample result

416256655364294967296 the above is how Python 3 creates iterators manually. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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