Why bool( object )?

Paul Rubin http
Fri May 1 04:56:50 EDT 2009

Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> (2) Why assume that a, b and c are sequences with a fast __len__ method? 
> They might be (say) linked lists that take O(N) to calculate the length, 
> or binary trees that don't even have a length, but can be iterated over.

Why assume they have a bool method?  Or a __or__ operator?

> for seq in [a,b,c]:
>     if len(seq) > 0:
>         for x in seq:
>             do_something_with(x)
>         break


   for seq in [a,b,c]:
     if sum(1 for x in imap(do_something_with, seq)) > 0:

More information about the Python-list mailing list