[Tutor] Looking to improve my stopWatch.py

Kent Johnson kent37 at tds.net
Thu Oct 25 12:51:29 CEST 2007


Dick Moores wrote:

> I've also seen things like this:
>  >>> a = []
>  >>> if not a:
> ...     print 'a is empty'
> ...
> a is empty
>  >>>
> 
> Isn't this preferable?:
>  >>> a = []
>  >>> if a == []:
> ...     print 'a is empty'
> ...
> a is empty

I like the simplicity and consistency of
   if a
or
   if not a

which will test for empty if a is a list, dict, string or set.

Of course strictly speaking
   if a
and
   if a == []
are not equivalent unless you can guarantee that a references a list 
(not None or any other kind of object) but for many purposes they are.

Kent
Kent


More information about the Tutor mailing list