On Oct 13, 2019, at 15:29, Chris Angelico
I don't often actually want to add to list-or-set, but I do sometimes have half-written code using one or the other, and then a future design constraint makes me rethink my choice of data type. Oh, now I have to change how I'm adding those items.
I sometimes run into that too, but usually it turns out to be a sign that what I really wanted was a generator function, and then passing the result of that function to the list or set constructor or to a list or set comprehension. Especially since it’s also usually around the time I realize that the 5-line loop I thought I had to write is 20 lines long and not even complete yet, so it’s calling out for refactoring into a function anyway. When for whatever reason it can’t be written that way, that’s often a sign that I need a collector function that takes an adder function, as the OP suggested later in the thread, rather than a function that just builds and returns a collection. The cases where it really is just a matter of wanting to change some too-small-to-refactor loop to build a set rather than a list, there’s probably only one append to change anyway. I’m sure that doesn’t cover 100% of all the times this comes up, but I think it covers so many of them that the need for a unified push function is very rare. If it had come up more than once or twice, I would have written that singledispatch push implementation long ago, but I’ve never thought of a need for it until this thread.