Coding style

Patrick Maupin pmaupin at gmail.com
Mon Jul 17 22:44:31 EDT 2006


PTY wrote:

> It looks like there are two crowds, terse and verbose.  I thought terse
> is perl style and verbose is python style.  BTW, lst = [] was not what
> I was interested in :-)  I was asking whether it was better style to
> use len() or not.

It's not canonical Python to use len() in this case.  From PEP 8:

- For sequences, (strings, lists, tuples), use the fact that empty
      sequences are false.

      Yes: if not seq:
           if seq:

      No: if len(seq)
          if not len(seq)

The whole reason that a sequence supports testing is exactly for this
scenario.  This is not an afterthought -- it's a fundamental design
decision of the language.

Regards,
Pat




More information about the Python-list mailing list