Continuing indentation

Pete Forman petef4+usenet at gmail.com
Wed Mar 2 19:23:37 EST 2016


Chris Angelico <rosuav at gmail.com> writes:

> On Thu, Mar 3, 2016 at 10:46 AM,  <codewizard at gmail.com> wrote:
>> On Wednesday, March 2, 2016 at 3:44:07 PM UTC-5, Skip Montanaro wrote:
>>>
>>>     if (some_condition and
>>>         some_other_condition and
>>>         some_final_condition):
>>>         play_bingo()
>>
>> How about:
>>
>>   continue_playing = (
>>       some_condition and
>>       some_other_condition and
>>       some_final_condition
>>   )
>>
>>   if continue_playing:
>>       play_bingo()
>>
>> or:
>>
>>   play_conditions = [
>>       some_condition,
>>       some_other_condition,
>>       some_final_condition,
>>   ]
>>
>>   if all(play_conditions):
>>       play_bingo()
>
> Those feel like warping your code around the letter of the law,
> without really improving anything.

I beg to differ. If an expression is long or complex then splitting it
up and, importantly, giving good names to the intermediates makes the
code clearer. That advice is not restricted to if statements.

-- 
Pete Forman



More information about the Python-list mailing list