Suggestions for good programming practices?

Aahz aahz at pythoncraft.com
Mon Jun 24 23:16:02 EDT 2002


In article <mailman.1024951532.5616.python-list at python.org>,
David LeBlanc <whisper at oz.net> wrote:
>Mark:
>>
>> When evaluating whether a variable is None, don't compare it like this:
>>
>> 	if x == None:
>>
>> instead, use:
>>
>> 	if x:
>>
>> or:
>>
>> 	if not x:
>>
>> The encourages a polymorphic approach.  If you must compare to
>> None, use the identify operator rather than equality:
>>
>> 	if x is None:
>>
>> or:
>>
>> 	if x is not None:
>
>I'm curious to know how this encourages polymorphism, and how making a
>conditional test implicit rather then explicit is good practice?

Depends on what you're testing for.  One of the big features of Python's
object model is that any "empty" object tests as false: None, 0, "", (),
[], and {}.  Testing for a specific false value (such as 0 or None)
prevents you from easily changing the object being used.  Rely on the
object to tell you whether it's true or false, and you'll be fine.

That's polymorphic.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list