Below I have some code:
class Boy:
def __init__(self,height,weight,limit):
self.height = height
self.weight = weight
self.limit = limit
self.count = 0
def __iter__(self):
return self
def __next__(self):
if self.count == self.limit:
print( 'Reached the limit')
raise StopIteration
current = self.count
self.count += 1
print( f'Iter {current}')
return 'Hey'
And if I do the following:
boy = Boy('140cm','25kg',limit=10)
for hey in boy:
print(boy)
The code above will output:
Iter 0
Hey
Iter 1
Hey
Iter 2
Hey
Iter 3
Hey
Iter 4
Hey
Iter 5
Hey
Iter 6
Hey
Iter 7
Hey
Iter 8
Hey
Iter 9
Hey
Reached the limit
But now if I add some code bellow (to collect all the 'Hey' in a list)
boy = Boy('140cm','25kg',limit=10)
for hey in boy:
print(boy)
boy_output = list(boy)
print(boy_output)
Now this code will output:
Iter 0
Hey
Iter 1
Hey
Iter 2
Hey
Iter 3
Hey
Iter 4
Hey
Iter 5
Hey
Iter 6
Hey
Iter 7
Hey
Iter 8
Hey
Iter 9
Hey
Reached the limit
Reached the limit
[]
Notice that the list is empty. But why? Is is because I ran a for loop? Is is something wrong with the instance once I ran a for loop? Or is it that I don't understand how custom iterators works?
The same thing if I did the opposite(storing the outputs first, then printing out the Heys and the output of the function. Which results in the function not printing out Hey, but the Heys are stored in the list):
Iter 0
Iter 1
Iter 2
Iter 3
Iter 4
Iter 5
Iter 6
Iter 7
Iter 8
Iter 9
Reached the limit
['Hey', 'Hey', 'Hey', 'Hey', 'Hey', 'Hey', 'Hey', 'Hey', 'Hey', 'Hey']
Reached the limit
What I want is to get the Heys from the instance(boy) and storing the Heys in a list(by collecting the output from the instance) at the same time
I think there is something wrong with the instance boy
, so are there anyway to fix that instances so that I can print all the Hey
and storing them at the same time?
The same thing goes for generators.
If anyone know how to solve the problem, pleas help me out. Thank you!