checking if a list is empty

Hans Georg Schaathun hg at schaathun.net
Wed May 11 05:14:38 EDT 2011


On 07 May 2011 02:49:53 GMT, Steven D'Aprano
  <steve+comp.lang.python at pearwood.info> wrote:
:  On Fri, 06 May 2011 16:05:09 -0400, Adam Tauno Williams wrote:
: 
: > I'd never accept code like "if not x" as an empty test.
: 
:  So much the worse for you then.
: 
:  The point of the "if x" idiom is that it is a polymorphic test which is 
:  independent of the type.

Normally, polymorphisms implies multiple forms only, where the different
forms has some form of common interpretation.  That's what makes
polymorphism useful and helpful, increasing legibility.

In this case, the interpretation of an arbitrary object as a boolean
is peculiar for python.  An empty list is a real, existing object, and 
the supposition that [] be false is counter-intuitive.  It can be
learnt, and the shorthand may be powerful when it is, but it will 
confuse many readers.

-- 
:-- Hans Georg



More information about the Python-list mailing list