jax is another example that uses the name 'set' for this functionality: https://jax.readthedocs.io/en/latest/jax.numpy.html
On second thought, perhaps it wouldn't be a bad idea to extend this proposal to built-in sequence types in general (list, str, tuple, and bytes). Examples of this functionality being used on those other types: https://sourcegraph.com/search?q=context:global+lang:python+%5C%5B%5Cs*:%5Cs*%28i%7Cj%7Ck%7Cind%7Cindex%29%5Cs*%5C%5D%5Cs*%5C%2B.*%5C%2B%5Cs*%5B%5Cw%5C.%5D%2B%5C%5B%5Cs*%28i%7Cj%7Ck%7Cind%7Cindex%29%5Cs*%5C%2B%5Cs*1%5Cs*:%5Cs*%5C%5D&patternType=regexp If extended beyond tuples, we would have to address the name collision with the existing 'replace' methods of str and bytes. In that case, something like 'set' or 'update' might work. (I prefer the former for its conciseness.) ------- Original Message ------- On Monday, March 14th, 2022 at 12:47 PM, wfdc <w...@protonmail.com> wrote: > > wfdc (should we call you something which is not a random-looking string of > > consonants?) > > wfdc is fine. > > > it might help if you could give examples of this functionality from other > > languages or classes. What do they call the method? > > https://clojuredocs.org/clojure.core/assoc > > https://immutable-js.com/docs/v4.0.0/set()/ > > http://swannodette.github.io/mori/#assoc > > https://hackage.haskell.org/package/lens-5.1/docs/Control-Lens-Operators.html#v:.-126- > > https://hackage.haskell.org/package/containers-0.6.5.1/docs/Data-Sequence.html#update > > What about 'update' or 'set'? > > > what if index assignment was an expression? [...] Now that I have seen this > > syntax, I want it!!! > > Interesting idea. Perhaps we can discuss it further. > > ------- Original Message ------- > > On Monday, March 14th, 2022 at 8:38 AM, Steven D'Aprano st...@pearwood.info > wrote: > > > 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/ _______________________________________________ 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/32V2TGUAVGJZQJ2NOYYJHNYYUTXXR545/ Code of Conduct: http://python.org/psf/codeofconduct/