On Fri, Mar 11, 2022 at 01:38:57AM +0000, Rob Cliffe via Python-ideas wrote:
> This could cause confusion because str.replace() has a completely > different API. The old "painter.draw()" versus "gun_slinger.draw()" problem. It does exist, but it's not generally a *major* problem. > 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) A reasonable suggestion. Does replace change items according to their position, or their value? Perhaps a better name would help. wfdc (should we call you something which is not a random-looking string of consonants?) it might help if you could give examples of this functionality from other languages or classes. What do they call the method? It would be nice to have a better API for this function. I have used it many times, for strings, lists and tuples, it is really a generate sequence operation: * make a copy of the sequence, replacing the item at position p with a new value; We can do it with type-specific expressions: seq[:p] + [value] + seq[p+1:] seq[:p] + (value,) + seq[p+1:] string[:p] + "c" + string[p+1:] but there is no good, type-agnostic way to easily do it. Wacky thought: what if index assignment was an expression? function(arg, seq[p]=value, another_arg) would pass a copy of seq with the item at p replaced by value. That would work with slices as well: seq[a:b] = values Now that I have seen this syntax, I want it!!! (It is not clear to me if this would be compatible to the existing `__setitem__ API.) > 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. Sure, not all one-liners need to be built-in. But some do. -- Steve _______________________________________________ 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/HKYHO35W5M3LKUGUZ6MRYVVEIKUQ5XHC/ Code of Conduct: http://python.org/psf/codeofconduct/