On Sun, Mar 5, 2017 at 12:16 PM, Ed Kellett <edk...@gmail.com> wrote:

> On Sun, 5 Mar 2017 at 19:54 David Mertz <me...@gnosis.cx> wrote:
>
>> In terms of an actual use case, I can see it for "Lists no longer than 4".
>>
> Any other use of this hypothetical method would be an anti-pattern
>>
>

> That's an excessively hard limit.
>

Maybe 5... in special circumstances :-)


> But the positions NEVER are important when you get to a 20 item list.  If
>> you design an argument parser that is looking for "the 17th argument" you
>> are doing it wrong.  I'm not saying that's impossible (nor even hard) to
>> program, but it's not good practice.
>>
>
> That's probably true of argument parsers, certainly not lookup tables.
>

I can think of a few special cases where index positions are useful.  But
they aren't common enough to warrant a new method, nor hard to do with the
existing language.  E.g.:

for i, data in enumerate(base_data):

    extra = extra_data[i] if len(extra_data) > i else DEFAULT

    combine(data, extra)


This might well use lists thousands of items long, and maybe `extra_data`
runs out before `base_data`.  That code would look very slightly nicer with
`extra_data.get()`.

On the other hand, better than either is:

from itertools import zip_longest

for data, extra in zip_longest(base, extra_data, fillvalue=DEFAULT):

    combine(data, extra)


So far no one in this thread has presented any (non-trivial) code that
would be better if `list.get()` existed.  I think I have personally come
closest, but I actively want it not to happen because it's an anti-pattern.

-- 
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@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to