[Python-Dev] string.find() again (was Re: timsort for jython)
Gordon McMillan
gmcm@hypernet.com
Mon, 5 Aug 2002 16:50:20 -0400
On 5 Aug 2002 at 16:15, Tim Peters wrote:
> [Tim]
> > I'd like to see a plausible use case for
> >
> > '' in str
> >
> > returning True, then.
>
> [Gordon McMillan]
> > Any code that currently does
> > str.find(x) >= 0
>
> You're saying that you actually do that in cases
> where x may be an empty string, and that it's useful
> to get a True result in at least one such case?
What I'm really saying is that I almost never use
x in str
because it's semantics have always been peculiar.
Thus, I don't *really* care whether '' in str raises
an exception, because if it does, I won't train myself
to use it <wink>.
[...]
> Sure -- that's what .find() is for, after all. But
> you're also saying that your algorithms expect to
> search for empty strings? Like in:
>
> index = option_letter_string.find(letter)
> if index >= 0:
> list_of_option_functions[index]()
> else:
> raise UnknownOptionLetter(letter)
>
> you make sure that list_of_option_functions[0] is
> suitable for processing both the first option in
> option_letter_string and an empty "option letter"?
Say we have a sequence of objects where obj.options
uses a string to hold (orthogonal) option codes. We're
selecting a subset based on the user's criteria, and
empty means "don't care".
Then
for obj in seq:
if obj.options.find(criteria):
rslt.append(obj)
makes perfect sense.
I rather doubt I have code in that exact
form, because I'd probably special case
it if it were that obvious.
if not criteria:
return seq
for obj in seq:
....
OTOH, I use find() a lot, and since I can't
recall having been bit by find('') returning 0, I
have to conclude that the mystically / mathematically
correct answer is, in my case at least, also
the pragmatically correct one.
But you solved a similar problem once
already, by noting that a large quantity had
to have at least 537 objects in it.
-- Gordon
http://www.mcmillan-inc.com/