On Thu, Aug 27, 2020 at 07:10:38PM +0200, Alex Hall wrote:
On Thu, Aug 27, 2020 at 6:32 PM Steven D'Aprano
wrote: Personally, I found your examples underwhelming because they're mostly repetitions of the same pattern.
That's surprising to me. When I see the same pattern over and over that's when I most want to refactor into a common function/method. The repetition is annoying and the solution is easy and obvious.
The repetition doesn't go away just because you give it a name. It's just easier to manage.
If the cases were all different it'd be much more complicated and chances are there wouldn't be a simple function that covered all cases.
Do you not usually refactor duplicated code? What *do* you refactor?
Depends on what the code is. If it's as trivial as a try...except with a single operation inside each block, it's hardly worth the bother to add an extra level of indirection, an extra function, just to reduce what is effectively a two-liner (albeit one which is commonly spread over four lines) to a one-liner: # Before try: x = L[index] except IndexError: x = "" # After x = get(L, index, "") Save a line of code, replace it with one more function to read, document and test. Instead of having to read a two (four) line try...except block where everything is explicitly in front of my eyes, I have to read a fairly bland and generic-looking "get" function where I'm not sure *precisely* what it does witout reading the docs. Get what? Does it support negative indices? Can it fail? If I change L to a deque will it continue to work? I'm not *against* it. I think if lists already had the method I wouldn't object to its existence. I'm just finding it hard to care, and I think that's probably the main issue you are up against. This doesn't have a compelling benefit, it's quite marginal, at least in the use-case above. That's my opinion, others may disagree.
I'd find it more interesting if there
were a larger variety of cases
This sort of sounds like the problem. The examples aren't meant to be interesting.
You're asking why people aren't interested in solving this issue, you wonder why few people are terribly interested in it, and now you're saying that its not supposed to be interesting. I think that explains the relative lack of interest :-) That's just my two cents though, others may feel differently. -- Steve