I appreciate the input and attention to detail!
Using the ``str()`` constructor was sort of what I had thought originally, and
that's why I had gone overboard with "casting" in one iteration of the sample
code. When I realized that this isn't quite "casting" and that ``__str__`` can
be overridden, I went even more overboard and suggested that
``str.__getitem__(self, ...)`` and ``str.__len__(self)`` could be written,
which does have the behavior of effectively "casting", but looks nasty. Do you
think that the following is a happy medium?
def removeprefix(self: str, prefix: str, /) -> str:
# coerce subclasses to str
self_str = str(self)
prefix_str = str(prefix)
if self_str.startswith(prefix_str):
return self_str[len(prefix_str):]
else:
return self_str
def removesuffix(self: str, suffix: str, /) -> str:
# coerce subclasses to str
self_str = str(self)
suffix_str = str(suffix)
if suffix_str and self_str.endswith(suffix_str):
return self_str[:-len(suffix_str)]
else:
return self_str
Followed by the text:
If ``type(self) is str`` (rather than a subclass) and if the given affix is
empty or is not found, then these methods may, but are not required to, make
the optimization of returning ``self``.
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/W6DMWMSF22HPKG6MYYCXQ6QE7QIWBNSI/
Code of Conduct: http://python.org/psf/codeofconduct/