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
 
11 7th December 03:05
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


  sponsored links


12 7th December 03:08
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
13 7th December 03:13
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
14 24th February 09:17
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
15 25th February 05:11
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
16 25th February 05:13
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
17 25th February 05:14
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
18 26th February 05:41
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
19 26th February 05:44
steve holden
External User
 
Posts: 1
Default Bug in string.find; was: Re: Proposed PEP: New style indexing,


We might agree, before further discussion, that this isn't the most
elegant part of Python's design, and it's down to history that this tiny little wart remains.

What I don't understand is why you want it to return something that
isn't a legal index. Before using the result you always have to perform
a test to discriminate between the found and not found cases. So I don't
really see why this wart has put such a bug up your ass.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
  Reply With Quote
20 26th February 05:44
bryan olson
External User
 
Posts: 1
Default Bug in string.find; was: Re: Proposed PEP: New style indexing,


I don't think my proposal breaks historic Python code, and I
don't think it has the same kind of unfortunate subtle
consequences as the current indexing scheme. You may think the
wart is tiny, but the duct-tape* is available so let's cure it.[*] http://www.google.com/search?as_q=warts+%22duct+tape%22


In this case, so that errors are caught as close to their
occurrence as possible. I see no good reason for the following
to happily print 'y'.

s = 'buggy' print s[s.find('w')]


The bug that got me was what a slice object reports as the
'stop' bound when the step is negative and the slice includes
index 0. Took me hours to figure out why my code was failing.

The double-meaning of -1, as both an exclusive stopping bound
and an alias for the highest valid index, is just plain whacked.
Unfortunately, as negative indexes are currently handled, there
is no it-just-works value that slice could return.


--
--Bryan
  Reply With Quote
Reply


Thread Tools
Display Modes




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