Einstein summation notation (was: question of style)

Paul Rubin http
Fri Jul 17 10:12:51 EDT 2009

Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> def assemble_page(header, body, footer):
>     if header or body or footer:
>         do_lots_of_expensive_processing()
>     else:
>         do_nothing_gracefully()

Why should the processing be expensive if all three fields are empty?

>     if header or body or footer:
> is a huge improvement over:
>     if (header != '') or (body != '') or (footer != ''):

Doesn't really seem any better.  There's always something like

   if any(p != '' for p in [header, body, footer, otherthing1, ...])

if the number of components gets larger.

More information about the Python-list mailing list