bool evaluations of generators vs lists

Josh Dukes josh.dukes at microvu.com
Tue Feb 10 14:15:14 EST 2009


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?

-- 

Josh Dukes
MicroVu IT Department



More information about the Python-list mailing list