[Python-ideas] get() method for list and tuples
mertz at gnosis.cx
Tue Feb 28 13:52:47 EST 2017
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas