bool evaluations of generators vs lists

J

Josh Dukes

quite simply...what???

In [108]: bool([ x for x in range(10) if False ])
Out[108]: False

In [109]: bool( x for x in range(10) if False )
Out[109]: True

Why do these two evaluate differently? I was expecting that they would
evaluate the same but the generator would return true *as soon as the
first value is detected*. I'd really expect it to act more like...

def has_values(g):
for i in g:
return True
return False

So what's going on here? Am I using the wrong function or is this
actually just a bug?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top