Movatterモバイル変換


[0]ホーム

URL:


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

David Mertzmertz 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 namedkey in a collection?"I'm all for duck typing, but it feels like those are a pretty differentpattern.  For example, I know that if a list has something at index 10 italso has something at index 9.  I absolutely *do not* know that if a dicthas something at key 'g' it also has something at key 'f'.-- Keeping medicines from the bloodstreams of the sick; foodfrom the bellies of the hungry; books from the hands of theuneducated; technology from the underdeveloped; and puttingadvocates of freedom in prisons.  Intellectual property isto 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/20170228/4bf2fe6c/attachment-0001.html>


More information about the Python-ideasmailing list

[8]ページ先頭

©2009-2026 Movatter.jp