[Python-ideas] get() method for list and tuples
Steven D'Aprano
steve at pearwood.info
Sat Mar 4 04:40:11 EST 2017
On Fri, Mar 03, 2017 at 10:35:18PM +0100, Michel Desmoulin wrote:
> Since the start of the discussion, contesters have been offering
> numerous solutions, all being contextual and with gotchas, none being
> obvious, simple or elegant.
I do not agree with that characterisation.
> The best is still try/except.
And I don't agree with that either.
> "There should be one obvious way to do it" right?
But what is "it" here?
Don't say "look up an arbitrary-indexed item which may not exist from a
sequence". That's too general, and in the most general case, the right
way to do that is to use sequence[index] which will raise if the item
doesn't exist. In other words, the status quo. Be specific. Show some
code -- its okay if its simplified code, but it should be enough to
demonstrate the *use-case* for this. "I have crappy JSON" is not a
use-case. How is it crappy and how would you use list.get to fix it?
This brings us back to the point I made really early on: this *seems*
like an obviously useful method, by analogy with dicts. I agree! It
*seems* useful, so obviously such that one of the first things I added
to my own personal toolbox of helper functions was a sequence get()
function:
def get(sequence, index, default=None):
try:
return sequence[index]
except IndexError:
return default
But then I never used it. "Seems useful" != "is useful", at least in my
experience.
> Plus Sven already estimated the implementation would not be very hard.
The simplicity of the implementation argues *against* the need for this
to be a built-in. If you really do need this, then why not add a
sequence get() function to your project? Its only five lines!
As far as I have seen, only one person apart from myself, Kyle
Lahnakoski, has implemented this helper in their own code. And Kyle
says he has talked himself out of supporting this change.
One thing I haven't seen is anyone saying "I am constantly writing and
re-writing this same helper function over and over again! I grepped my
code base and I've recreated this helper in 30 different modules.
Maybe it should be a built-in?" That would be a good argument, but
nobody has made it. Lots of people saying that they desperately need
this method, but apparently most of them don't need it enough to write a
five line helper function to get it. They'd rather wait until they've
migrated all their code to Python 3.7.
> So we have one obvious solution to a problem that:
>
> - several professional programmers said they have
I'm not convinced by claims that "I need to fetch arbitrary indexes from
sequences ALL THE TIME, sorry I can't show any examples..."
> - has a similar API in another built-in
> - has currently no elegant solutions
>
> The proposal is actionable, the cost of it seems low, and it's not
> remotely controversial.
It is only "not remotely controversial" if you ignore all those who
disagree that this is needed.
> I get that on Python-idea you get "no" by default, but here we are
> having such resistance for a feature that is light to implement, does
> not clutter anything, does solve a problem, and is congruent with other
> APIs.
>
> Honestly what evil would happen if it's get accepted ?
>
> This is not a "yeah but we can't accept everything that goes in or we
> would bloat Python" thing.
Yes it is.
But fundamentally, although I really don't see the benefit to this, I'm
not *strongly* against it either. I don't think the sky will fall if it
is added to sequences. But if somebody wants to code this up (don't
forget the Sequence ABC) and submit a patch or a PR for a senior
developer to look up, I'm not going to deny you that opportunity,
--
Steve
More information about the Python-ideas
mailing list