What does <generator object <generator object <genexpr> at 0x0402C7B0> mean?
p.f.moore at gmail.com
Wed May 22 06:25:59 EDT 2019
On Wed, 22 May 2019 at 02:45, Terry Reedy <tjreedy at udel.edu> wrote:
> On 5/21/2019 9:11 PM, CrazyVideoGamez wrote:
> > I tried doing a list comprehension. I typed:
> > favorite_fruits = ['watermelon', 'blackberries']
> > print(fruit for fruit in favorite_fruits)
> > And I got:
> > <generator object <genexpr> at 0x0402C7B0>
> > What does this mean
> It means that the expression (fruit for fruit in favorite_fruits)
> evaluates to a generator object.
> > and what do I have to fix?
> Perhaps you wanted to run the generator, perhaps like this:
> >>> favorite_fruits = ['watermelon', 'blackberries']
> >>> print(*(fruit for fruit in favorite_fruits))
> watermelon blackberries
Or maybe you just wanted a loop?
for fruit in favorite_fruits:
Generator and list comprehension syntax is a somewhat more advanced
feature of Python (not *very* advanced, particularly in the case of
list comprehensions, but enough so to be tricky for a newcomer). I
don't know how much experience the OP has with Python, but as a
general rule, it's always better to stick with the simplest approach
that does what you want, and statements like loops are simpler than
complex one-line expressions (even if the complex one liners make you
look cool ;-))
More information about the Python-list