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

Michael Powe michael at trollope.org
Fri Jun 22 22:06:18 EDT 2001


>>>>> "John" == John J Lee <phrxy at csv.warwick.ac.uk> writes:

    John> 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
    John> [...]

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

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

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

Why?  You already assume that 0 is not false.  That would not be
changed. 

mp

-- 
  Michael Powe                                 Portland, Oregon USA
'Unless we approve your idea, it will not be permitted, it will not be
allowed.'  -- Hilary Rosen, President, Recording Industry Association
of America



More information about the Python-list mailing list