[Python-ideas] New list methods
Steven D'Aprano
steve at pearwood.info
Wed May 6 01:31:46 CEST 2009
On Wed, 6 May 2009 06:20:31 am Carl Johnson wrote:
> Would there be any interest in adding a 'reversed' kwarg to the
> relevant string methods, deprecating the r-methods in Python 3.2, and
> removing them in Python 3.3? It might make things a little simpler
> and unclutter the dir for strings a bit…
-0.5 from me. I think a "reversed" argument clutters up the signature of
the various find() methods (etc) for little or no benefit, and
certainly not enough benefit to make up for the hassle of going through
the depreciation process and breaking compatibility with existing code.
A "reversed" kwarg would only be useful when you don't know which
direction you want to search in until runtime. How often does that
happen? In practice, I believe you will nearly always know whether you
want to search from the left or the right when you're writing the code.
so I expect the "reversed" kwarg will nearly always be given as a
constant:
astring.find("spam", reversed=True)
as opposed to:
astring.find("spam", reversed=(today==Wednesday))
A slightly whimsical example, but I hope it illustrates the point.
On the rare occasion that you do need a flag to distinguish "operate
from the left" from "operate from the right", I don't believe it is a
hardship to use something like the following idiom:
def func(astring, arg, ... , reversed=False):
if reversed: find = astring.find
else: find = astring.rfind
...
n = find(arg)
--
Steven D'Aprano
More information about the Python-ideas
mailing list