On 11/03/2022 02:19, wfdc wrote:
> This could cause confusion because str.replace() has a completely different
API.
We're talking about tuples here, not strings.
Yes, I know, and tuples and strings are different. But *humans* can be
confused by "replace" having a totally different API in different
contexts. This suggests at least finding a different name for your
proposed method.
Saying that a method's API differs for a completely different type,
especially when such a difference would be expected given the
difference in types, is not a valid objection.
> And indeed if a replace method were added to tuples, a fair case could be made for it having the same API, viz.
replace(old, new, count=-1)
Not sure what you mean by this. Please clarify.
A function that replaced tuple elements by value rather than by index
would no doubt be useful to somebody (not that I'm proposing it). So it
could use the same API as str.replace().
> Whereas your suggestion can be written as a simple 1-liner, as you demonstrate. So there is no strong need for a new
method for it.
The same can be said for index and count, along with numerous other
methods attached to Python's built-in types.
Hm, off the top of my head, I could (I believe) write "count" as an
(inefficient) 1-liner, but not "index". I suggest it's harder than you
think. (Try it!)
Something being simple to implement does not mean it shouldn't be
built-in.
See Python's "batteries included" philosophy.
"Not every 1-line function needs to be a built-in".
If users find themselves re-implementing the same utility function
over again and over again across different projects, it's a good sign
that such a function should be part of the standard library.
Well, you are 1 user. Have you evidence that there are (many) others?
Best wishes
Rob Cliffe
------- Original Message -------
On Thursday, March 10th, 2022 at 8:38 PM, Rob Cliffe via Python-ideas
<python-ideas@python.org> wrote:
This could cause confusion because str.replace() has a completely
different API.
And indeed if a replace method were added to tuples, a fair case
could be made for it having the same API, viz.
replace(old, new, count=-1)
Whereas your suggestion can be written as a simple 1-liner, as you
demonstrate. So there is no strong need for a new method for it.
Best wishes
Rob Cliffe
On 10/03/2022 03:42, wfdc via Python-ideas wrote:
Add a "replace" method to tuples that returnsa new tuple with the
element at a given index replaced with a given value. Example
implementation:
def replace(self, index, value):
return self[:index] + (value,) + self[index + 1:]
See
https://stackoverflow.com/questions/11458239/how-to-change-values-in-a-tuple
for more context.
Currently, tuples have 2 public methods: index and count. replace
would be similarly easy to implement and similarly useful.
Furthermore, it would be a natural counterpartto nametuple's
_replace method.
_______________________________________________
Python-ideas mailing list --python-ideas@python.org
To unsubscribe send an email topython-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived
athttps://mail.python.org/archives/list/python-ideas@python.org/message/TIHIG74ADMMZRKRNGR2TDZSXIMM6N7JM/
Code of Conduct:http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/QXB2HFMGAWDEI7G56ULOZIMW7LHR5CRY/
Code of Conduct: http://python.org/psf/codeofconduct/