[Python-ideas] get() method for list and tuples
Nick Coghlan
ncoghlan at gmail.com
Thu Mar 2 01:10:55 EST 2017
On 2 March 2017 at 07:03, David Mertz <mertz at gnosis.cx> wrote:
> 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've never wanted a `list.get`, but I have occassionally wished that:
1. operator.get/set/delitem were available as builtins (like
get/set/delattr)
2. the builtin getitem accepted an optional "default" argument the way
getattr does
That is, the desired common operation isn't specifically
"obj.get(subscript)" or "obj.get(subscript, default)", it's:
_raise = object()
def getitem(container, subscript, default=_raise):
try:
return container[subscript]
except LookupError:
if default is _raise:
raise
return default
Mappings just happen to already offer that functionality as a method.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170302/2491b4e8/attachment-0001.html>
More information about the Python-ideas
mailing list