Checking if elements are empty

Neil Cerutti horpner at yahoo.com
Mon Sep 10 15:06:35 CEST 2007


On 2007-09-08, Wildemar Wildenburger <lasses_weil at klapptsowieso.net> wrote:
> Lawrence D'Oliveiro wrote:
>>>>> if y[0]:
>>>> Not a good idea.
>>> Why not?
>> 
>> Because there is a situation where your version of the test
>> will fail even if the first element of y is non-null.
>
> Such as? Seriously people, a little more verbosity wouldn't
> hurt here. This isn't a mystery game.

>>> if "": True
...
>>> if 0: True
...
>>> if []: True
...
>>> if {}: True
...
>>> if None: True
...

That's may not be all the possibilities. Lots of Python objects
can evaluate to false in a boolean context.

-- 
Neil Cerutti
The Pastor would appreciate it if the ladies of the congregation would lend
him their electric girdles for the pancake breakfast next Sunday morning.
--Church Bulletin Blooper



More information about the Python-list mailing list