Einstein summation notation (was: question of style)

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jul 17 09:28:00 EDT 2009


On Fri, 17 Jul 2009 00:34:00 -0700, Paul Rubin wrote:

> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
>> It is very useful to be able to write e.g.: 
>>
>> if header or body or footer:
>>     print assemble_page(header, body, footer)
>>
>> and have empty strings to be equivalent to False.
> 
> Why doesn't assemble_page properly handle the case where header, body,
> and footer are all empty?  That would let you eliminate the if.  "Make
> sure your code 'does nothing' gracefully" (Kernighan and Plauger).

You mean something like this?

def assemble_page(header, body, footer):
    if header or body or footer:
        do_lots_of_expensive_processing()
    else:
        do_nothing_gracefully()

Sure, why not?

Whatever way you do it, my point is that it is very useful to perform 
branching tests on non-Boolean objects. Wherever you put the test, being 
able to write:

    if header or body or footer:

is a huge improvement over:

    if (header != '') or (body != '') or (footer != ''):



-- 
Steven



More information about the Python-list mailing list