[Python-ideas] get() method for list and tuples

Ed Kellett edk141 at gmail.com
Sun Mar 5 08:03:33 EST 2017


On Sun, 5 Mar 2017 at 11:27 Paul Moore <p.f.moore at gmail.com> wrote:

> On 3 March 2017 at 18:29, Ed Kellett <edk141 at gmail.com> wrote:
>
> - Which of the existing things (slice + [default], conditional on a slice,
> > conditional on a len() call) do you think is the obvious way to do it?
>
> Write a small wrapper function that implements the functionality
> (however you want, but it doesn't have to be a single expression, so
> you've much more flexibility to make it readable) and then use that.
>

It's hardly a question of readability at that point. A reader is at the
very least going to have to look at the signature of the utility function
in order to be sure about argument order.

> - Are there any examples where list.get would be applicable and not the
> > obviously best way to do it?
>
> I don't understand the question. If you're asking which is better
> between list.get and a custom written function as described above,
>

No. I'm asking: if list.get did exist, are there any cases (compatibility
with old versions aside) where list.get's semantics would be applicable,
but one of the alternatives would be the better choice?

"writing a helper function" is a generally
> useful idiom that works for many, many things, but list.get only
> solves a single problem and every other such problem would need its
> own separate language change.


Custom helper functions can obviously accomplish anything in any language.
If we had to choose between def: and list.get, I'd obviously opt for the
former.

The disadvantage that you have to write
> the helper is trivial, because it's only a few lines of simple code:
>

I don't think the size of a helper function is relevant to how much of a
disadvantage it is. Most built-in list methods are trivial to implement in
Python, but I'm glad not to have to.

Ed
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170305/bf5414ba/attachment-0001.html>


More information about the Python-ideas mailing list