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

Nick Timkovich prometheus235 at gmail.com
Tue Feb 28 16:02:51 EST 2017


I wouldn't see myself using it for an arbitrary value in the middle of the
list, but perhaps for the 0th or -1st (first/last) element of a list that
might be empty. Maybe also second-to-first/last if I'm initializing some
comparison between sequential items?

Some of the examples don't work with negative indexes, boo.

On Tue, Feb 28, 2017 at 12:52 PM, David Mertz <mertz at gnosis.cx> wrote:

> On Tue, Feb 28, 2017 at 10:10 AM, Sven R. Kunze <srkunze at mail.de> wrote:
>
>> Yes, and easily written as above.  What significant advantage would it
>> have to spell the above as:
>>
>>     x = alist.get(pos, default_val)
>>
>> It's a couple characters shorter in the proposed version.  I guess I'll
>> concede that needing the odd indexing at the end to get the scalar is
>> slightly ugly.
>>
>> 1. advantage: it looks like dict access -> allows duck typing (oh how
>> often I'd missed that)
>> 2. advantage: no try except
>> 3. advantage: no weird workaround with slices and additional item access
>>
>
> How often would you duck-type "access either an integer position or a
> named key in a collection?"
>
> I'm all for duck typing, but it feels like those are a pretty different
> pattern.  For example, I know that if a list has something at index 10 it
> also has something at index 9.  I absolutely *do not* know that if a dict
> has something at key 'g' it also has something at key 'f'.
>
>
> --
> 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.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170228/a39b4dc1/attachment-0001.html>


More information about the Python-ideas mailing list