On Thu, Mar 05, 2020 at 12:45:28PM -0800, Andrew Barnert via Python-ideas wrote:
Well, I like the idea if someone can come up with a good naming scheme—something that at least reminds me which function is the “set of chars” stripper and which the “substring” stripper,
You've been a Python programmer for how many years now? Do you currently have trouble remembering what lstrip and rstrip do? If not, I doubt you will suddenly forget their meaning because we add a pair of new methods with quite different names. Unless, of course, we go down the path of a foolish consistency and choose names which are too similar: lcut lremove ldelete in which case you are right, people would certainly confuse the two. So let's not do that :-) This proposal isn't for a generic "substring" stripper. We already have that, it is called str.replace. This proposal is specifically for removing *prefixes and suffixes* not arbitrary substrings. Referencing the words "prefix" and "suffix" is not "gratuitously" adding "yet another way to say l/left/start" but a critical part of the methods' semantics. If we were to describe what the proposed methods do, we surely would say something like these: cut the prefix delete the prefix remove the prefix strip the prefix trim the prefix because that is what the method does: it cuts/deletes etc the *prefix*, not some arbitrary substring or set of characters. But we wouldn't say: cut the substring on the left because "on the left" is not sufficient. How far on the left? Can the substring be anywhere in the left half of the string? "The red peppers are very red and fresh".cut_left_substring("red") => "The peppers are very red and fresh" I hope that we can agree that some names are simply too long and cumbersome: str.cut_substring_all_the_way_on_left(prefix) We already have `[l|r]strip` methods. If we want to associate the new methods with those, I suggest strip_prefix strip_suffix which will show up right next to `strip` in the docs and state explicitly what they do. (Personally, I prefer to add a little more conceptual distance by calling the method "cut_" but I'm willing to accept "strip" if others like it.) I trust that is clear enough. If not, what else could "strip prefix" mean, if not strip the prefix? Contrast: lstrip_string "Yes, I know it operates on a string, it's a string method after all. But what does it do?" The following is a tangential note about some Python history, and isn't really relevant to the proposal as such. So you can stop reading here without missing anything important. [Christopher]
1) I don’t think any other string methods take keywords.
[Andrew]
Sure, but they mostly go back to the days before keyword arguments existed at all, much less now when they can be implemented relatively easily even in C functions.
Keywords predate string methods. String methods were only added in Python 2.0: https://docs.python.org/2.0/lib/string-methods.html In Python 1.5, strings had no methods: >>> 'a'.upper() Traceback (innermost last): File "<stdin>", line 1, in ? AttributeError: 'string' object has no attribute 'upper' and we used the "string" module functions. I don't see any builtins with keyword arguments in Python 1.5: https://docs.python.org/release/1.5/lib/node26.html but pure Python functions certainly had them. Since string functions were originally written in Python, they could have had keyword arguments had they been desired. -- Steven