if the else short form

Antoon Pardon Antoon.Pardon at rece.vub.ac.be
Wed Oct 6 08:56:22 CEST 2010


On Wed, Oct 06, 2010 at 01:45:51PM +1300, Lawrence D'Oliveiro wrote:
> In message <mailman.1339.1286268545.29448.python-list at python.org>, Antoon 
> Pardon wrote:
> 
> > On Tue, Oct 05, 2010 at 06:55:33PM +1300, Lawrence D'Oliveiro wrote:
> >
> >> In message <mailman.1232.1285927634.29448.python-list at python.org>, Antoon
> >> Pardon wrote:
> >> 
> >> > On Wed, Sep 29, 2010 at 01:38:48PM +0200, Hrvoje Niksic wrote:
> >> >
> >> >> BTW adding "==True" to a boolean value is redundant and can even break
> >> >> for logically true values that don't compare equal to True (such as
> >> >> the number 10 or the string "foo").
> >> > 
> >> > But leaving it out can also break things.
> >> 
> >> Only in code which was incorrectly written to begin with.
> > 
> > Well you can always define such code as incorrectly written of course.
> 
> In this situation, I mean by ???incorrectly written??? code which uses non-
> Boolean values as conditional expressions.

Please be more specific:

A lot of times someone comes with code like the following:

  if len(lst) != 0:
    ...


and than gets the advise to write it as follows:

  if lst:
    ...

Do you mean that this second piece of code is incorrectly written,
since it uses a non-boolean value as conditional expression?

-- 
Antoon Pardon



More information about the Python-list mailing list