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

David Mertz mertz at gnosis.cx
Wed Mar 1 16:03:32 EST 2017


On Wed, Mar 1, 2017 at 11:13 AM, Abe Dillon <abedillon at gmail.com> wrote:

> I'd like to +1 this proposal. It should be trivial to implement. It won't
> break backward compatibility. It's intuitive. I can think of several places
> I would use it. I can't think of a good reason not to include it.
>

I've yet to see in this thread a use case where list.get() would make
sense.  Specifically, I've yet to see a case where there is straightforward
duck-typing substitutability between dicts and lists where you'd want that.

I saw something that said "semi-structured data sources like JSON are often
messy and you need lots of try/except blocks."  But that's really not the
same thing. Even if some node in a structure might variously be a list,
dict, or scalar (or other types; sets?), I haven't seen any code where this
hypothetical list.get() would improve that problem.

In contrast, *iteration* is definitely a case where I often want to freely
substitute lists, sets, dicts, and other collections.  They share that
natural capability, so being able to type `for x in collection:` is a good
generic win to have.

As I've said, the huge difference is that the "keys" to a list have a clear
and obvious relationship amongst themselves.  They are always successive
non-negative integers, with the minimum always being zero.  That makes a
whole lot of operations and assumptions very different from dictionaries
whose keys are completely independent of each other.  If `mylist[N]` works,
`mylist[N-1]` cannot fail with an IndexError (obviously unless the list is
mutated in between those operations); there's nothing remotely analogous
for dictionaries, and that's the reason we have dict.get().

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170301/b495076d/attachment.html>


More information about the Python-ideas mailing list