Request for feedback on my first Python program

David Glass nospam at nospam.ca
Sun Jun 1 13:33:59 EDT 2003


Jack Diederich wrote:
> On Sun, Jun 01, 2003 at 12:02:56PM -0400, Aahz wrote:
> 
>>In article <mailman.1054429562.11692.python-list at python.org>,
>>Sean Legassick  <sean at datamage.net> wrote:
>>
>>>In message <slrnbdeevn.694.andrew-usenet at athena.jcn.srcf.net>, Andrew 
>>>Walkingshaw <andrew-usenet at lexical.org.uk> writes
>>>
>>>>def isMeaningful(line):
>>>>   if line != "" and line[0] != "#":
>>>>       return True
>>>>   else:
>>>>       return False
>>>
>>>Yuck!
>>>
>>>def isMeaningful(line):
>>>    return line != "" and line[0] != "#";
>>>
>>>No offence intended, but I've seen this too often in real code not to 
>>>point out its redundancy here.
>>
>>It is *NOT* redundant; it is making clear that True/False are the only
>>possible return values.  With your rewrite, reading and understanding
>>the code is necessary to clarify the intent.
> 
> 
> +1 for good advice
>  
> If the docstring says "returns true if some_condition, false otherwise"
> invariably someone will look at the code and use the side effect that it
> isn't returning 1/0 for evil purposes[1].  reversing the order of compairson
> shouldn't break any code, so it is good defensive programming to always
> explicity return a bool.
> 
> The snippet in question will always return a bool, but IMO this is rarely
> the case so it is best to be explicit.
> 
> -jack
> 
> [1] shoot the person who depended on the undocumented side effect
>

How about this as a compromise (using Python 2.2; maybe works with 
earlier versions):

def isMeaningful(line):
     return bool(line != "" and line[0] != "#")

- David





More information about the Python-list mailing list