Printing a generator returns "<generator object _as_iterable at 0x7f0476373e10>", need to print its values

Steve D'Aprano steve+python at pearwood.info
Mon Dec 5 17:49:50 EST 2016


On Tue, 6 Dec 2016 05:39 am, vmahajan at centerpointmedia.com wrote:

> Can someone help me print a generator object?

The same way as you print any other object:

print(obj)  # Python 3

print obj  # Python 2


But what you're actually asking for is a way to print the values produced by
the generator object. You do that the same way as you would print the
values produced by any sequence or iterable. You can print one value per
line:

for value in obj:
    print(value)


Or you can convert to a list, and then print the list:

print(list(obj))


Or you can format it yourself, any way you like, by writing some code:


display_string = '::'.join(str(value).upper() for value in obj)
print('[[' + display_string + ']]')




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.



More information about the Python-list mailing list