
On 30 September 2011 16:30, Tarek Ziadé <ziade.tarek@gmail.com> wrote:
Hey,
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
starts = ('a', 'b', 'c') somestring = 'acapulco'
for start in starts: if somestring.startswith(start): print "yeah"
So what about a startsin() method, that would iterate over a sequence:
if somestring.startsin('a', 'b', 'c'): print "yeah"
if any(somestring.startswith(s) for s in starts) print "yeah"
Implementing it in C should be faster as well
Probably slightly, but how critical is speed here?
same deal with .endswith I guess
A benefit of the any() recipe is that it generalises, so you don't need to have endsin as well, (or whatever else people might think of...) But it is a common pattern, and there's also the case where you want to know *which" pattern matched, which any can't do, so there may be some mileage in this. But it might be simpler just to accept that you've reached the point where a simple regex is the best answer. Yes, now you have two problems, I know :-) Paul.