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

Ed Kellett edk141 at gmail.com
Sun Mar 5 14:22:28 EST 2017


On Sun, 5 Mar 2017 at 18:13 David Mertz <mertz at gnosis.cx> wrote:

> But if the next line is:
>
>     data = args.get(17, "<empty>")
>
> Then I'm pretty sure the programmer thinks she's being passed a very
> different type of collection than is actually available.  I'd rather that
> fails right away and in an obvious way then silently produce a value.
>

That's up to the programmer. args[17] exists and does fail immediately. If
the programmer provides a default value, presumably they know they want one.


> Specifically, if I think I'm dealing with a list that is likely to have 20
> items (rather than maybe 4 or fewer), I'm almost sure the best way to deal
> with it is in a loop (or comprehension, map(), etc) and NOT by poking into
> large index positions that may or may not be present.
>

I really think that depends what it's a list of. If the positions of things
in the list are important (as with an argument parser, or perhaps a lookup
table) I fail to see why it would be wrong to peek.

If lists were really designed to be used only as you and some others in
this thread are suggesting, I don't think they'd have indexed access at all.

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


More information about the Python-ideas mailing list