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

John J. Lee phrxy at csv.warwick.ac.uk
Fri Jun 22 17:34:39 EDT 2001


On Fri, 22 Jun 2001, Jeff Shannon wrote:
[...]
> Except that, even if string.find() returned None (false), it still
> wouldn't work:
>
>     s = "spam"
>     sub = "sp"
>     if string.find(s, sub):
>         dospam()
>
> In this case, dospam() never executes, even though our substring *is*
> found.
>
> While you *could* explicitly test
>
>     if string.find(s, sub) is not None:
>
> I don't see this as being really any better than
>
>     if string.find(s, sub) >= 0:
>
> or
>
>     if string.find(s, sub) == -1
[...]

In fact, I think you've just demonstrated why -1 is actually *better* than
None.  If it did return None, people would forever be testing

if string.find(s, sub):
    foo()

and forgetting about the 0 case.  At least, I would be, I'm sure.


John




More information about the Python-list mailing list