Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52352

Re: Python Basic Doubt

Date 2013-08-10 21:35 -0600
From Michael Torrie <torriem@gmail.com>
Subject Re: Python Basic Doubt
References <CAL0E0u6wO_UBniWoSpePvhKhPDG_nf4p1rqYYrGwzoHTqp6ZHA@mail.gmail.com> <20130810114040.6ac78fe8@bigbox.christie.dr> <CAL0E0u69NXDrtojK4dUY+EcP5e9ab5BYkXb-M3Dz=+BTWpKmSA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.459.1376192149.1251.python-list@python.org> (permalink)

Show all headers | View raw


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 = []

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python Basic Doubt Michael Torrie <torriem@gmail.com> - 2013-08-10 21:35 -0600

csiph-web