[Python-ideas] An iterable version of find/index for strings?

Andrew Barnert abarnert at yahoo.com
Mon Apr 8 20:04:40 CEST 2013


On Apr 8, 2013, at 8:23, "Stephen J. Turnbull" <stephen at xemacs.org> wrote:

> Andrew Barnert writes:
> 
>> Yes, but regular expressions shouldn't be the one way to do a
>> simple text search!
> 
> Why not?  I don't see a real loss to "match('^start')" vs
> "startswith('start')" in terms of difficulty of learning, and a
> potential benefit in encouraging people to avail themselves of the
> power of regexp search and matching.

I don't see how you could think these are equally easy to learn. You could show the latter to someone who's never written a line of code and they'd already understand it.

But that's not the important part. The benefit of startswith is that it takes less effort to read and understand the code. Reading a regex, or s[:5]=='start' for that matter, isn't _hard_, but it still takes a bit of mental effort, which slows you down a little bit, which limits how much code you can understand in one scan. There's also the fact that there's literally nothing to get wrong with startswith, which means when you're debugging your code, you don't have to mentally check a regex or slice to make sure it's right. One of the great things about python is that you can often understand what a function does, and be sure it's correct, in just a glance.

Also, the fact that new programmers can use python for serious work (even text processing work) before they've learned regex is a strength, not a weakness. If you have to tell people "before you can parse that log/csv/whatever you have to learn how to escape parentheses and create matching groups", you might as well teach them perl.


More information about the Python-ideas mailing list