[Python-3000] Small renaming suggestion: re.sub() -> re.replace() or re.substitute()
Mark Summerfield
mark at qtrac.eu
Fri Oct 5 09:20:39 CEST 2007
Hi,
It seems to me that one of the few really "bad" method names in the
Python library that I regularly encounter is re.sub().
I don't like the name because:
(1) It is an abbreviation, but not an "obvious" one like max and min
(2) It is an ambiguous name: could be substitute or could be subtract
(3) Elsewhere where special method __foo__ that implements a named (as
opposed to symbol-based) method, that method is called foo. For
example, __cmp__() -> cmp(), __int__() -> int(), __len__() -> len().
But __add__ -> +, __sub__() -> -.
(4) It is the only function with this name in the library; whereas there
are several replace methods:
bytes.replace()
str.replace()
datetime.date.replace()
# and a few others, plus some replace_* functions.
Although re.substitute() would work (and be better than sub), I think
re.replace() is better and more consistent regarding the rest of the
library.
And as for subn, well, replacen or substituten are possible, but why not
have just one method and have an optional keyword argument if a tuple is
wanted?
--
Mark Summerfield, Qtrac Ltd., www.qtrac.eu
More information about the Python-3000
mailing list