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-valu
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
dem
> 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 val
> 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.
I agree with this. It was also earlier specified:
> Furthermore,
On Fri, 11 Mar 2022 at 14:36, Jeremiah Vivian
wrote:
> > 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 s
> On the other hand, it might be an indication that a tuple is the wrong tool
> for the job.
1. It's not. The original tuple is not being mutated. And it may be desirable
to enforce that immutability at the type level. Hence the choice of tuple
rather than, say, list.
2. The same "objection" w
I'm pretty wary of adding methods to builtins -- I wonder how often tuples
are either subclassed or duck-typed if the answer isn't "virtually never",
then it's a problem to add to the API.
That being said, I do find myself wanting to change just one element of a
tuple pretty frequently, and it alw
>
>
> > On the other hand, it might be an indication that a tuple is the wrong
> tool for the job.
>
maybe -- if I"m doing a lot of replacing, I likely would turn to a list --
but if you need an immutable, you need an immutable. I'm pretty sure I've
written code like:
temp = list(a_tuple)
temp[i]