◐ Shell
clean mode source ↗

Allow `bytes.replace`'s 'count' to be a keyword argument

Feature or enhancement

Proposal:

Allow the 'count' argument of bytes.replace to be a keyword to better describe its use.

Since Python 3.13, the 'count' of str.replace can be a keyword argument to make the code more verbose. However, the same is not true for the 'count' argument of bytes.replace, which is still enforced to be a positional argument:

>>> 'aa'.replace('a', 'b', count=1)
'ba'
>>> b'aa'.replace(b'a', b'b', count=1)
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    b'aa'.replace(b'a', b'b', count=1)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
TypeError: bytes.replace() takes no keyword arguments
>>> b'aa'.replace(b'a', b'b', 1)
b'ba'

Allowing 'count' to be a keyword argument here would allow writing more verbose code as well as make the standard library APIs more consistent.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Suggested for str.replace in this issue: #106487

Linked PRs