Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Programming languages > Bug in string.find was: Re: Proposed PEP: New style indexing was
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 11th October 17:25
bryan olson
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing was



The doc for the find() method of string objects, which is
essentially the same as the string.find() function, states:

find(sub[, start[, end]])
Return the lowest index in the string where substring sub
is found, such that sub is contained in the range [start,
end). Optional arguments start and end are interpreted as
in slice notation. Return -1 if sub is not found.

Consider:

print 'Hello'.find('o')

or:

import string
print string.find('Hello', 'o')

The substring 'o' is found in 'Hello' at the index -1, and at
the index 4, and it is not found at any other index. Both the
locations found are in the range [start, end), and obviously -1
is less than 4, so according to the do***entation, find() should
return -1.

What the either of the above actually prints is:

4

which shows yet another bug resulting from Python's handling of
negative indexes. This one is clearly a do***entation error, but
the real fix is to cure the wart so that Python's behavior is
consistent enough that we'll be able to describe it correctly.


--
--Bryan
  Reply With Quote


  sponsored links


2 11th October 17:26
steve holden
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing



Do you just go round looking for trouble?

As far as position reporting goes, it seems pretty clear that find()
will always report positive index values. In a five-character string
then -1 and 4 are effectively equivalent.

What on earth makes you call this a bug? And what are you proposing that
find() should return if the substring isn't found at all? please don't
suggest it should raise an exception, as index() exists to provide that
functionality.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
  Reply With Quote
3 11th October 17:29
casey hawthorne
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing was Re: Bug in slice type


Does range(start, end) generate negative integers in Python if start

--
Regards,
Casey
  Reply With Quote
4 11th October 17:31
enkarpachov
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style


On Thu, 25 Aug 2005 00:05:18 -0400

Returning -1 looks like C-ism for me. It could better return None when none
is found.

index = "Hello".find("z")
if index is not None:
# ...

Now it's too late for it, I know.

--
jk
  Reply With Quote
5 20th October 22:13
paul rubin
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing was Re: Bug in slice type


Steve Holden <steve@holdenweb.com> writes:

Bryan is making the case that Python's use of negative subscripts to
measure from the end of sequences is bogus, and that it should be done
some other way instead. I've certainly had bugs in my own programs
related to that "feature".
  Reply With Quote
6 22nd October 01:42
bryan olson
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing


Steve Holden asked:


In the course of programming, yes, absolutly.

What you just said, versus what the doc says.


There are a number of good options. A legal index is not one of
them.


--
--Bryan
  Reply With Quote
7 23rd October 06:22
antoon pardon
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing was Re: Bug in slice type


Op 2005-08-25, Bryan Olson schreef <fakeaddress@nowhere.org>:

IMO, with find a number of "features" of python come together.
that create an awkward situation.

1) 0 is a false value, but indexes start at 0 so you can't
return 0 to indicate nothing was found.

2) -1 is returned, which is both a true value and a legal
index.


It probably is too late now, but I always felt, find should
have returned None when the substring isn't found.

--
Antoon Pardon
  Reply With Quote
8 23rd October 06:28
bryan olson
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing


None is certainly a reasonable candidate. The one-past-the-end
value, len(sequence), would be fine, and follows the preferred
idiom of C/C++. I don't see any elegant way to arrange for
successful finds always to return a true value and unsuccessful
calls to return a false value.

The really broken part is that unsuccessful searches return a
legal index.

My suggestion doesn't change what find() returns, and doesn't
break code. Negative one is a reasonable choice to represent an
unsuccessful search -- provided it is not a legal index. Instead
of changing what find() returns, we should heal the
special-case-when-index-is-negative-in-a-certain-range wart.


--
--Bryan
  Reply With Quote
9 24th October 09:04
rick wotnaz
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing was Re: Bug in slice type


Practically speaking, what difference would it make? Supposing find
returned None for not-found. How would you use it in your code that
would make it superior to what happens now? In either case you
would have to test for the not-found state before relying on the
index returned, wouldn't you? Or do you have a use that would
eliminate that step?

--
rzed
  Reply With Quote
10 7th December 03:03
bryan olson
External User
 
Posts: 1
Default Bug in string.find was: Re: Proposed PEP: New style indexing was


The doc for the find() method of string objects, which is
essentially the same as the string.find() function, states:

find(sub[, start[, end]])
Return the lowest index in the string where substring sub
is found, such that sub is contained in the range [start,
end). Optional arguments start and end are interpreted as
in slice notation. Return -1 if sub is not found.

Consider:

print 'Hello'.find('o')

or:

import string
print string.find('Hello', 'o')

The substring 'o' is found in 'Hello' at the index -1, and at
the index 4, and it is not found at any other index. Both the
locations found are in the range [start, end), and obviously -1
is less than 4, so according to the do***entation, find() should
return -1.

What the either of the above actually prints is:

4

which shows yet another bug resulting from Python's handling of
negative indexes. This one is clearly a do***entation error, but
the real fix is to cure the wart so that Python's behavior is
consistent enough that we'll be able to describe it correctly.


--
--Bryan
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666