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/67N5KSTPQJHWXMSA322QEYYBK26SBXZW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to