Skip to content

Commit 164f8fa

Browse files
authored
Make the specification more concise (#1344)
1 parent f53a04e commit 164f8fa

File tree

1 file changed

+7
-40
lines changed

1 file changed

+7
-40
lines changed

pep-0616.rst

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ requires a module import, and results in less readable code.
4949
Specification
5050
=============
5151

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``::
5454

5555
def removeprefix(self: str, prefix: str, /) -> str:
5656
if self.startswith(prefix):
@@ -59,45 +59,20 @@ following behavior::
5959
return self[:]
6060

6161
def removesuffix(self: str, suffix: str, /) -> str:
62+
# suffix='' should not call self[:-0].
6263
if suffix and self.endswith(suffix):
6364
return self[:-len(suffix)]
6465
else:
6566
return self[:]
6667

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.
7670

7771
Methods with the corresponding semantics will be added to the builtin
7872
``bytes`` and ``bytearray`` objects. If ``b`` is either a ``bytes``
7973
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.
10176

10277

10378
Motivating examples from the Python standard library
@@ -233,14 +208,6 @@ expansion of the ``lstrip``/``rstrip`` API -- repeatedly applying the
233208
function until the argument is unchanged. This behavior is attainable
234209
from the proposed behavior via by the following::
235210

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-
244211
>>> s = 'Foo' * 100 + 'Bar'
245212
>>> prefix = 'Foo'
246213
>>> while s.startswith(prefix): s = s.removeprefix(prefix)

0 commit comments

Comments
 (0)