Another Wart! string.find() [ was: namespace issue? ]

David Bolen db3l at fitlinxx.com
Thu Jun 21 19:55:53 EDT 2001


"Steven D. Majewski" <sdm7g at Virginia.EDU> writes:

>   This is an illogical convention, however it would be very difficult
>   to root out, because, as you have noted, everyones code is FULL 
>   of explicit checks that string.find does not return -1. 
> 
>   I'ld love to see it fixed, however, I suspect that in this case, 
>   many lines of installed code trumps Computer Programming for Everybody! 

Particularly since any change to something like None could break a lot
of that code, since I'm sure code exists that assumes the result is an
integer.

At least part of the reason this particular method tends to require a
few more brain cycles when I use it is that I tend to think of "find"
as more intuitively being a boolean sort of operation - e.g., "is this
string in this other string", while the function actually acts more as
what I would have probably called "index".  Using the latter name makes
me think more immediately of a numeric return, at which point a -1
convention for no possible index is just fine.

But I have lots of code that really just uses find to determine if the
substring exists (and doesn't actually care where), so you end up with
a ">= 0" check (or equivalent) in many places where it would be
unnecessary if the method was really a boolean check.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list