Iterators Tutorial

This commit is contained in:
Corey Schafer
2018-10-23 17:44:00 -06:00
parent 9e4da1f06e
commit 588f06736b
2 changed files with 70 additions and 0 deletions
@@ -0,0 +1,41 @@
class Sentence:
def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()
def __iter__(self):
return self
def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]
def sentence(sentence):
for word in sentence.split():
yield word
my_sentence = sentence('This is a test')
# for word in my_sentence:
# print(word)
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
# This should have the following output:
# This
# is
# a
# test