MrJoe
MrJoe

Reputation: 445

Printing generator objects in python

I created my own iterator (to learn how they work) as follows

class Reverse():
    def __init__(self, word):
        self.word = word
        self.index = len(word)
    def __iter__(self):
        return self
    def __next__(self):
        self.index -=1
        if self.index < 0:
            raise StopIteration
        return self.word[self.index]

print (char for char in Reverse("word"),end="")

I know I can say:

rev = Reverse("word")
for char in rev:
    print(char, end="")

but I was hoping to be able to do it in a single line

print (char for char in Reverse("word"),end="")

This doesn't work and raises an error. If I remove the end="" it prints out a generator object. Surely by including a for loop in the print statement it should iterate through my generator object and print each item? Why does this not happen

Upvotes: 0

Views: 4487

Answers (4)

Robo
Robo

Reputation: 29

x = my_objects.objects.all() # x, turns to a QuerySet

for i in range(len(x)):
    print(x[i])

Upvotes: 1

Victor Hugo Borges
Victor Hugo Borges

Reputation: 413

Why not like this?

print(''.join([char for char in Reverse("word")]))

Upvotes: 1

N Chauhan
N Chauhan

Reputation: 3515

A generator is a lazy iterator. When you use a for loop, you are manually exhausting the generator. When you print it out in a single line, you are not exhausting it by iterating.

You can unpack the generator to exhaust it and get all of the values:

print(*(char for char in Reverse('word')), sep='')

I’ve changed end for sep as you are doing everything in 1 print call so want to separate each ‘argument’ (character) with no space.

You can also do the following:

print(*Reverse('word'), sep='')

As Reverse is already an iterator, you are able to exhaust it.

Upvotes: 2

ForceBru
ForceBru

Reputation: 44838

Surely by including a for loop in the print statement it should iterate through my generator object and print each item?

No, it will print the string representation of a generator object.

If you want to output what this object generates, you can use a starred expression:

print(*(char for char in Reverse("word")),end="")

This is because the for loop in a generator expression isn't actually executed at the instantiation of the generator object, and that's the whole point of generators: you can have an infinite generator and still be able to pass it around as a regular value and consume finite chunks of it, all that without having infinite amounts of memory.

Upvotes: 1

Related Questions