Sideway
output.to from Sideway
Draft for Information Only

Content

Python Iterator Types
 Iteration Methods
  Iteration of Container Objects
   container.__iter__()
  Iteration of Iterator Objects
   iterator.__iter__()
   iterator.__next__()
 Generator Types
 Sequence Types
 Source and Reference

Python Iterator Types

Python iterator ia a type used to implement the concept of iteration over containers. There are two distinct methods for allowing user-defined classes to support iteration.

Iteration Methods

Methods of interation are used to support container objects and iterator objects.

Iteration of Container Objects

One method is used to provide iteration support.

container.__iter__()

Return an iterator object. The object is required to support the iterator protocol described below. If a container supports different types of iteration, additional methods can be provided to specifically request iterators for those iteration types. (An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.) This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.

Iteration of Iterator Objects

Two methods are used to provide basic method that forming the iterator proctocol.


Once an iterator’s __next__() method raises StopIteration, it must continue to do so on subsequent calls. Implementations that do not obey this property are deemed broken.

iterator.__iter__()

Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.

iterator.__next__()

Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.

Generator Types

Python’s generators provide a convenient way to implement the iterator protocol. If a container object’s __iter__() method is implemented as a generator, it will automatically return an iterator object (technically, a generator object) supplying the __iter__() and __next__() methods.

Sequence Types

Python provides several basic iterator objects as sequence types to support specific Python sequence types manipulation. The basic sequence types are
  • lists
  • tuples
  • range objects.
Other tailored sequence types are specially designed for processing of binary data and text strings etc.

Source and Reference


©sideway

ID: 210100008 Last Updated: 1/8/2021 Revision: 0


Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019