API symmetry

Symmetric APIs are easier to use. I was reminded of this when doing some regular expression programming in Python and comparing it to Perl. Perl’s regular expression operators for search and replace are symmetric in a way that their Python counterparts are not.

Perl uses m/pattern/ for matching and s/pattern/replacement/ for substitution. Both apply to the first instance of a pattern in a string by default. The g option following a match or substitute operator causes the command to apply to all instances of the pattern. The i option after either a match or substitute command causes the pattern to apply in a case-insensitive manner. Matching and substitution are symmetric.

Python uses re.search() for matching and re.sub() for substitution. The search function can only apply to the first instance of a pattern; to match all instances of a pattern, use re.findall(). The function re.sub() applies to all instances by default, but it has a max parameter that can be set to limit the number of instances it applies to. To make a search pattern case-insensitive, pass in re.IGNORECASE flag. To make a substitution case-insensitive, modify the regular expression itself by adding (?i).

In general, I find Python syntax much cleaner than Perl, but regular expressions are implemented more elegantly in Perl.

Tagged with: , , ,
Posted in Python, Software development
2 comments on “API symmetry
  1. Gene Harris says:

    What did one regex say to the other?
    .+

  2. i’ve had the same issue with python regex library. it’s very minorly misdesigned, but misdesigned enough that it’s hard to use.

    i recently tried writing a light wrapper around it that makes argument orders and such more reasonable. i need to write more examples but the working code right now is at http://anyall.org/sane_re.py .

2 Pings/Trackbacks for "API symmetry"
  1. [...] API symmetry Adding simplicity ? X [...]

  2. [...] the prefixes ltp and utp are easy to remember if you know what they stand for. They’re more symmetric than, for example, Python’s cdf and [...]