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

Donn Cave donn at u.washington.edu
Fri Jun 22 14:59:20 EDT 2001


Quoth "Steven D. Majewski" <sdm7g at Virginia.EDU>:
| On 22 Jun 2001, Donn Cave wrote:
| > 
| > Given that you can't treat find or index as a boolean, there is
| > no fault I can see with its -1 failure return.  In fact it would
| > be worse if it were false.  Usage that doesn't work reliably
| > ideally shouldn't work at all, or it's all the harder to diagnose.
| > 
|
| That last sentence above is just why I suggested None would be a 
| better return value. You still can't do "if not string.find(..." 
| because zero is still a valid return offset, but the problem with
| -1 is that it can slip thru since [-1]  is a valid string index, 
| it's valid in a slice expression, and can be added or subtracted
| as an offset to another index value, all without giving an exception
| anywhere near the error. If you try to use None as an index or add
| it to another offset, you get an exception right away. 

So we could enjoy the best of both if it returned 'not found'.
That would support neither incorrect boolean use nor careless
use of invalid return.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list