"if {negative}" vs. "if {positive}" style

Martin P. Hellwig martin.hellwig at dcuktec.org
Wed Feb 10 09:40:44 CET 2010

On 02/10/10 03:36, Tim Chase wrote:
> Larry Hudson wrote:
>> But a minor rearrangement is simpler, and IMHO clearer:
>> if 'mystring' not in s:
>> print 'not found'
>> else:
>> print 'foundit'
>> print 'processing'
> I've always vacillated on whether that would better be written as Larry
> does, or as
> if 'mystring' in s
> print 'foundit'
> print 'processing'
> else:
> print 'not found'

> Any thoughts on how others make the choice?
I cases like this when aesthetics are not that much of a deal breaker, I 
usually put the condition which I think will be the true the majority of 
the time first.
This is not for performance reasons (never tested whether this has any 
effect) but more documentation like purpose to go from a most expected 
case to the least expected one. YMMV


More information about the Python-list mailing list