Skip to main content
Data Science Wizardry Blog by Attila Vajda

Representations of generator objects are not errors.

>>> print(["\n".join("*" * i) for i in range(4)])
['', '*', '*\n*', '*\n*\n*']

I wonder if it is possible to write print(x for x in range(4))

>>> print(x for x in range(4))
<generator object <genexpr> at 0x108429580>

At first I thought this was an error, but it is the representation of a lazy generator object. It is akin to a piano that is not being played.

Making progress 😌:

>>> print("\n".join(["*" * i for i in range(4)]))

*
**
***