-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Asynchronous Programming in Python
By :
Iteration is one of the basic operations in imperative programming. Besides iterating over the standard data structures provided by the Python programming interface, you can develop your own iterator classes. These must implement the iterator contract, which involves implementing the __iter__ and __next__ methods.
The following code, available at Chapter 3/iterators.py, implements an iterator over a fictional soccer team’s members. Data is generated on-the-fly to look like real data; this synthetic data could be used for integration tests, in which you want data that is close to real data in terms of format and statistical properties but which is otherwise unrelated to real data (usually for compliance and/or security reasons).
from faker import Faker
class SoccerTeam():
def __init__(self):
fake = Faker()
self.players = [fake.name() for _ in range(11)]
self.current = 0
def __iter__(self):
...