Boolean tests [was Re: Attack a sacred Python Cow]

Carl Banks pavlovevidence at gmail.com
Wed Jul 30 01:33:50 EDT 2008


On Jul 30, 1:15 am, "Russ P." <Russ.Paie... at gmail.com> wrote:
> All this discussion about "if x" has me wondering. I use it all the
> time because a fully explicit test just seems redundant. But maybe it
> does have some value in terms of readability and type checking. After
> all, it is possible to forget whether something is a number or a list.

IMO, the only time I think "if x" is really dangerous is if you are
using "if x" instead of "if x is None" to distinguish between None and
a type that can have false values.  Well, also it can backfire to use
"if x" to check whether an iterable is empty since you can get a false
positive if it's an iterator.

Other than that it's maybe slighly less type safe to use explicit
test.


> Having said that, it would sure be nice to be able to write
>
> if myList is not empty:
>
> instead of
>
> if len(myList) != 0:

I can agree with this.


Carl Banks



More information about the Python-list mailing list