Skip to Content
Secret Recipes of the Python Ninja
book

Secret Recipes of the Python Ninja

by Cody Jackson
May 2018
Intermediate to advanced content levelIntermediate to advanced
380 pages
9h 37m
English
Packt Publishing
Content preview from Secret Recipes of the Python Ninja

How iteration works in Python

In Python, an iterator is an object that represents a stream of data. While iterators are available for containers, sequences in particular always support iteration.

Iterators have the __next__() method available (or the built-in next() function). Calling next() multiple times returns successive items from the data stream. When no more items are available, a StopIteration exception is thrown.

Any class can use an iterator by defining a container.__iter__() method. This method returns an iterator object, typically just self. This object is necessary to support the iterator protocol. Different types of iteration can be supported, with each one providing a specific iterator request. For example, a tree structure ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Expanding World of Python

The Expanding World of Python

Dane Hillard

Publisher Resources

ISBN: 9781788294874Supplemental Content