Python Basic Doubt

Michael Torrie torriem at gmail.com
Sat Aug 10 23:35:31 EDT 2013


On 08/10/2013 09:09 PM, Krishnan Shankar wrote:

> i.e. Is this code possible
> 
> if a is False:
>     print 'Yes'
> if b is False:
>     print 'No'
> 
> Because i recommended this should not be done. But my colleagues say it is
> correct.

You are probably correct in your believe that this idiom should be
avoided.  As Chris says, it's much more pythonic to just use if not a.

There is one case where the recommended idiom is to use the 'is'
operator. That's when you want an empty list as a default parameter to a
function.  Since lists are mutable, often times using [] as a default
parameter is the wrong thing to do.  This is the recommended idiom:

def my_func(mylist = None):
    if mylist is None:
        mylist = []




More information about the Python-list mailing list