Home  >  Article  >  Backend Development  >  Python Programming: Iterable Objects and Iterators (Iterable & Iterator)

Python Programming: Iterable Objects and Iterators (Iterable & Iterator)

王林
王林forward
2023-04-20 12:19:061302browse

Preface

In Python programming, loop processing is inevitable, which involves two important concepts, namely iterable objects and iterators (Iterable & Iterator). I will introduce this topic of iteration in two to three parts to help you understand and apply it in actual programming. This article is the first.

Python Programming: Iterable Objects and Iterators (Iterable & Iterator)

Iterable object

Iterable object (Iterable) refers to an object that can return one member at a time. Examples of iterable objects include all sequence types (such as list, str, and tuple) and some non-sequence types such as dict, file objects, and any object of a class you define that implements the __iter__() method or __getitem__ The sequence semantics specified by the () method - and the iteration protocol (see here for details: ).

Iterable objects can be used in for loops and other places where sequences are required (such as zip(), map(), filter()...). When an iterable object is passed as a parameter to the built-in function iter(), it returns an iterator of that object. This iterator is only suitable for one-time traversal of a set of values. When working with iterable objects, you generally don't need to call iter() or handle the iterator object yourself. This is done automatically using a for statement, which creates a temporary unnamed variable that is used to hold the iterator during the loop.

In short, an iterable object is any class object that implements the __iter__() method or the __getitem__() method.

Iterator

Iterator (Iterator) refers to an object that represents a data flow. Repeatedly calling the iterator's __next__() method (or passing it to the built-in function next()) will return consecutive items in the stream. When no more data is available, the StopIteration exception is raised. At this point, the iterator object has been exhausted and any further calls to its __next__() method will raise StopIteration again. Iterators must have an __iter__() method that returns the iterator object itself, so every iterator is also iterable and can be used in most places that accept other iterable objects.

In short, an iterator is a data flow object that implements two iterator protocol methods __next__() and __iter__().

A notable exception is code that attempts to iterate multiple times (using the same iterator multiple times). A container object (such as a list) generates a new iterator every time it is passed to the iter() function or used in a for loop. Trying this with an iterator will simply return the exhausted iterator object used during the previous iteration, making it look like an empty container. So avoid this situation when writing code.

iter function

In addition, you need to understand that the built-in function iter() (which calls the __iter__() method behind the scenes) returns an iterator object. So we can say. An iterable is an object that returns an iterator.

In order to further understand iterators and iterable objects, I will follow up with explanations and code examples to further introduce the following key points:

  • Iterator protocol and loops
  • Implement custom iterator.

The above is the detailed content of Python Programming: Iterable Objects and Iterators (Iterable & Iterator). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete