0

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!

Tian
  • 31
  • 5
  • Yes, generators are "exhausted" when they are iterated over. – Selcuk Jul 30 '21 at 02:31
  • Hmmmmm... So there is no way to fix this. I mean I could just make another instance of the function, but what if I don't want to? – Tian Jul 30 '21 at 02:40
  • The duplicate question discusses ways to implement what you are looking for. The simplest way in your case would be to simply convert it to a list from the beginning and iterate over the list twice, as a list won't be exhausted like a generator (in other words, use `boy_output` in your `for` loop as well). – Selcuk Jul 30 '21 at 02:47
  • But why are they "exhausted"? – Tian Jul 30 '21 at 02:54
  • Because Python won't store the old values somewhere unless you manually do. In your case it might be possible to reset the generator, but not in general (imagine a generator spitting out random numbers; you won't get the same ones after you reset it and iterate over it a second time). – Selcuk Jul 30 '21 at 02:55

0 Answers0