6/07/2020

Python - Coding Problem: Creating Your Own Iterators

** Coding Problem: Creating Your Own Iterators


==========================================

my_sentence = Sentence('This is a test')

for word in my_sentence:
print(word)


# This should have the following output:
# This
# is
# a
# test

---------------------------------

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]


my_sentence = Sentence('This is a test')

for word in my_sentence:
print(word)

---------------------------------

This
is
a
test

==========================================
==========================================

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]


my_sentence = Sentence('This is a test')

print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))

---------------------------------

is
a
test
    print(next(my_sentence))
  File "C:\Users\purunet\Documents\py9\ii.py", line 14, in __next__
    raise StopIteration
StopIteration

---------------------------------

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)

---------------------------------

This
is
a
test

==========================================