Python style guidelines

Donn Cave donn at u.washington.edu
Thu Mar 11 11:49:54 EST 2004


In article <EO%3c.2675$G3.20988 at localhost>,
 Peter Hansen <peter at engcorp.com> wrote:

[... re "if a.find(v):"]

> I believe it's more likely they just forgot what find() did return, as 
> they were writing the code.  I've done the same, thinking it was a 
> boolean, not thinking that it returned a negative but that negatives 
> were considered false.

I'm sure that's common.  In principle, seems to me that in
many if not most cases the "index" function expresses the
result better - for example,

   try:
       t = a.index(e)
   except ValueError:
       return a
   else:
       return a[:t]

But for some reason, the average programmer seems to greatly
prefer non-valid error returns over exceptions, even though
they're naturally idiosyncratic and prone to this kind of
error.  I sure don't recall seeing "index" very often, anyway.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list