[BangPypers] Truth Tests (bool)

Saager Mhatre saager.mhatre at gmail.com
Fri Jan 13 14:24:27 CET 2012


On Wed, Jan 11, 2012 at 9:49 AM, Noufal Ibrahim <noufal at nibrahim.net.in>wrote:

> Gora Mohanty <gora at mimirtech.com> writes:
>
>
> [...]
>
> > It is not clear what you want from your description.  To check if a
> > list is empty, see if len( list ) is zero.
>
> [...]
>
> You don't want to do that. Your "list" might be a generator (unless you
> check for type which is a bad idea anyway) and "len"ing that will
> consume it which might be a potentially expensive operation.


Noufal,
Noticed something interesting as I was updating
truth.py<https://gist.github.com/1327614#file_truth.py> to
include generators and generator expressions; the `len` function doesn't
handle generators at all.

$ *python -c 'len(i for i in range(10))'*
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: object of type 'generator' has no len()


I'm pretty sure you just meant 'iterable' where you wrote 'generator', or
didn't you?

- d


More information about the BangPypers mailing list