Arithmetic with Boolean values

Roy Smith roy at panix.com
Sun Aug 12 07:40:30 EDT 2012


In article <502791ea$0$29978$c3e8da3$5496439d at news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:

> for x in (0,) if len(L)%2 else (0, 1):
>     ...
> 
> which is even more explicit and simpler to read even though it is longer.

Ugh.

do_stuff()
if len(L) % 2 == 0:
   do_stuff()  # reprocess even-length list

Sure, it's 3 lines instead of one, but dead-obvious what the intention 
is.  I might even go for:

if len(L) % 2:
   do_stuff()
else:
   do_stuff()
   do_stuff()

There's two problems with all the looping suggestions people have given.  
One is that the computation of whether you need to do it once or twice 
is messy.  But, but bigger issue is you're trying to warp what's 
fundamentally a boolean value into a looping construct.  That's a 
cognitive mismatch.



More information about the Python-list mailing list