Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre The Iterator Protocol | Understanding Iterators
Efficient Data Handling in Python

bookThe Iterator Protocol

Understanding how Python handles iteration is essential for working with sequences, loops, and many built-in features. At the core of this functionality is the iterator protocol. The iterator protocol is a set of methods that allows objects to be iterated over, meaning you can use them in a for loop or with functions like list() and sum(). In Python, an object is considered iterable if it implements the __iter__ method, which should return an iterator. An iterator is an object that implements both the __iter__ and __next__ methods. The __next__ method returns the next item in the sequence, and raises a StopIteration exception when there are no more items.

123456789101112131415161718
class CountUpTo: def __init__(self, max): self.max = max self.current = 1 def __iter__(self): return self def __next__(self): if self.current > self.max: raise StopIteration value = self.current self.current += 1 return value counter = CountUpTo(3) for number in counter: print(number)
copy

In this example, the CountUpTo class is a custom iterator. It keeps track of the current number and stops iteration when it reaches the maximum value. By defining both __iter__ and __next__, this class can be used in a for loop, which will automatically call __iter__ to get the iterator and then repeatedly call __next__ to retrieve each value.

1. Which of the following best describes the purpose of the __iter__ method in the iterator protocol?

2. Which of the following objects in Python are considered iterable?

question mark

Which of the following best describes the purpose of the __iter__ method in the iterator protocol?

Select the correct answer

question mark

Which of the following objects in Python are considered iterable?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain the difference between an iterable and an iterator in Python?

How does the `StopIteration` exception work in this example?

Can you show how to manually use the iterator without a for loop?

bookThe Iterator Protocol

Glissez pour afficher le menu

Understanding how Python handles iteration is essential for working with sequences, loops, and many built-in features. At the core of this functionality is the iterator protocol. The iterator protocol is a set of methods that allows objects to be iterated over, meaning you can use them in a for loop or with functions like list() and sum(). In Python, an object is considered iterable if it implements the __iter__ method, which should return an iterator. An iterator is an object that implements both the __iter__ and __next__ methods. The __next__ method returns the next item in the sequence, and raises a StopIteration exception when there are no more items.

123456789101112131415161718
class CountUpTo: def __init__(self, max): self.max = max self.current = 1 def __iter__(self): return self def __next__(self): if self.current > self.max: raise StopIteration value = self.current self.current += 1 return value counter = CountUpTo(3) for number in counter: print(number)
copy

In this example, the CountUpTo class is a custom iterator. It keeps track of the current number and stops iteration when it reaches the maximum value. By defining both __iter__ and __next__, this class can be used in a for loop, which will automatically call __iter__ to get the iterator and then repeatedly call __next__ to retrieve each value.

1. Which of the following best describes the purpose of the __iter__ method in the iterator protocol?

2. Which of the following objects in Python are considered iterable?

question mark

Which of the following best describes the purpose of the __iter__ method in the iterator protocol?

Select the correct answer

question mark

Which of the following objects in Python are considered iterable?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 1
some-alt