
On 05/10/2011 18:56, Ethan Furman wrote:
Nick Coghlan wrote:
On Oct 5, 2011 10:32 AM, "Ethan Furman" wrote:
Ron Adam wrote:
On Wed, 2011-10-05 at 19:08 +1300, Greg Ewing wrote:
Guido van Rossum wrote:
I'm out of ideas here. But of all these, str.find is probably still the worst -- I've flagged bugs caused by it too many times to count.
Could a with-statement be used here somehow?
with finding(x, s) as i: ...
Or an iterator.
for i in finding(x, s): ...
How would the case of not found be handled in either of these proposals?
By never executing the body of the loop. It's still a thoroughly unnatural API for the 0 or 1 case, though.
Let me rephrase:
found = "I don't want to get into the cart!".find('z') if found >= 0: # do stuff if found else: # do stuff if not found
or
found = "I don't want to get into the cart!".find('n') while found >= 0: # do stuff if found found = "I don't want to get into the cart!".find('n', found+1) if found == -1: break else: print('false branch') # do stuff if not found
How would we reliably get the false branch with the above proposals?
We've had the discussion before about how to handle the case when the body of the loop isn't executed at all.
I had the thought that a possible syntax could be:
found = "I don't want to get into the cart!".find('n') while found >= 0: # do stuff if found found = "I don't want to get into the cart!".find('n', found+1) or: print('false branch') # do stuff if not found
but I think I'll leave it there.