On Wed, 6 May 2009 10:45:32 am George Sakkis wrote:
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.
Sorry, that doesn't make much sense. Not only list.sort()/sorted() use exactly this API and we (thankfully) don't have list.rsort()/rsorted(),
list.sort() and sorted() are a very different situation. There was no depreciated reversesort() function to worry about when the reverse param was added. What we used to have is alist.sort() alist.reverse() For obvious reasons, that can't be written as alist.sort().reverse() Personally, I don't dislike the two-line version, but given that sorted() has a reverse flag, I accept that sort() should get one too for consistency. As for sorted(), remember that reversed() returns an iterator, not a list. So without a reverse param, you would need to write: list(reversed(sorted(alist))) but that's nasty for something as common as reversing a sorted list.
but that argument could be used against almost all functions with boolean parameters, since they are typically called with a constant True or False [1].
And I would exactly make that argument. What other built-in functions take a bool param to select between different functionality? sorted() is a special case. Anything else? I can't think of anything off the top of my head, but maybe that's just me. While I won't put words into Guido's mouth, I believe he has made that argument in the past. If your function takes a bool param to switch between two different behaviours, that's a good sign that it should be two functions rather than one. -- Steven D'Aprano