> This could cause confusion because str.replace() has a completely different
> API.
We're talking about tuples here, not strings.
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.
> 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.
Something being simple to implement does not mean it shouldn't be built-in.
See Python's "batteries included" philosophy.
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.
------- 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 returns a 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 counterpart to nametuple's _replace
>> method.
>>
>> _______________________________________________
>> 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/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/AUHKWFBYGEKZBEYBE56SH2YZJF5QCNOV/
Code of Conduct: http://python.org/psf/codeofconduct/