@@ -49,8 +49,8 @@ requires a module import, and results in less readable code.
49
49
Specification
50
50
=============
51
51
52
- The builtin ``str `` class will gain two new methods with roughly the
53
- following behavior ::
52
+ The builtin ``str `` class will gain two new methods which will behave
53
+ as follows when `` type(self) is type(prefix) is type(suffix) is str `` ::
54
54
55
55
def removeprefix(self: str, prefix: str, /) -> str:
56
56
if self.startswith(prefix):
@@ -59,45 +59,20 @@ following behavior::
59
59
return self[:]
60
60
61
61
def removesuffix(self: str, suffix: str, /) -> str:
62
+ # suffix='' should not call self[:-0].
62
63
if suffix and self.endswith(suffix):
63
64
return self[:-len(suffix)]
64
65
else:
65
66
return self[:]
66
67
67
- Note that ``self[:] `` might not actually make a copy -- if the affix
68
- is empty or not found, and if ``type(self) is str ``, then these methods
69
- may, but are not required to, make the optimization of returning ``self ``.
70
- However, when called on instances of subclasses of ``str ``, these
71
- methods should return base ``str `` objects, not ``self ``.
72
-
73
- Without the check for the truthiness of ``suffix ``,
74
- ``s.removesuffix('') `` would be mishandled and always return the empty
75
- string due to the unintended evaluation of ``self[:-0] ``.
68
+ These methods, even when called on ``str `` subclasses, should always
69
+ return base ``str `` objects.
76
70
77
71
Methods with the corresponding semantics will be added to the builtin
78
72
``bytes `` and ``bytearray `` objects. If ``b `` is either a ``bytes ``
79
73
or ``bytearray `` object, then ``b.removeprefix() `` and ``b.removesuffix() ``
80
- will accept any bytes-like object as an argument. Although the methods
81
- on the immutable ``str `` and ``bytes `` types may make the aforementioned
82
- optimization of returning the original object, ``bytearray.removeprefix() ``
83
- and ``bytearray.removesuffix() `` should *always * return a copy, never the
84
- original object.
85
-
86
- To test whether any affixes were removed during the call, users
87
- may use the constant-time behavior of comparing the lengths of
88
- the original and new strings::
89
-
90
- >>> string = 'Python String Input'
91
- >>> new_string = string.removeprefix('Py')
92
- >>> modified = (len(string) != len(new_string))
93
- >>> modified
94
- True
95
-
96
- Users may also continue using ``startswith() `` and ``endswith() ``
97
- methods for control flow instead of testing the lengths as above.
98
-
99
- The two methods will also be added to ``collections.UserString ``, with
100
- similar behavior.
74
+ will accept any bytes-like object as an argument. The two methods will
75
+ also be added to ``collections.UserString ``, with similar behavior.
101
76
102
77
103
78
Motivating examples from the Python standard library
@@ -233,14 +208,6 @@ expansion of the ``lstrip``/``rstrip`` API -- repeatedly applying the
233
208
function until the argument is unchanged. This behavior is attainable
234
209
from the proposed behavior via by the following::
235
210
236
- >>> s = 'Foo' * 100 + 'Bar'
237
- >>> prefix = 'Foo'
238
- >>> while len(s) != len(s := s.removeprefix(prefix)): pass
239
- >>> s
240
- 'Bar'
241
-
242
- or the more obvious and readable alternative::
243
-
244
211
>>> s = 'Foo' * 100 + 'Bar'
245
212
>>> prefix = 'Foo'
246
213
>>> while s.startswith(prefix): s = s.removeprefix(prefix)
0 commit comments