An .get method wont hurt duck typing, it makes it better. Also most of the
time we're working with plain dicts and lists not abtract classes.

If you're typechecking for a Sequence you cant treat it as a list, and this
is already true because there are methods in list that dont exists Sequence
as Alex said. Also the idea was always for a list, tuples and ranges are
okay, but tuples are immutable and ranges are constructed so it's not that
useful, as it is for lists derived from some unknown input

It's simple:

def get(self, i, default=None):
    try:
        return self[i]
    except IndexError:
        return default

That was the idea, just a safe wrapper arround IndexError. Because it's
very common to need to catch this. I posted two bugs that happened because
of uncaught IndexErrors.

.get() enforces better coding, it's more readable, avoid distracting
indentation blocks and flow jumping, the error handling is reduced and more
explicity and more fluid, you can get a fine default if you want. Besides
breaking Sequences contract on list, (which is already broken as Alex said,
by .copy, < and other methods) I cant see any other problem for adding it
to *lists*




Em qui, 27 de ago de 2020 05:57, Alex Hall <alex.moj...@gmail.com> escreveu:

> On Thu, Aug 27, 2020 at 10:15 AM Stephen J. Turnbull <
> turnbull.stephen...@u.tsukuba.ac.jp> wrote:
>
>> Alex Hall writes:
>>  > On Wed, Aug 26, 2020 at 5:00 PM Stephen J. Turnbull <
>>  > turnbull.stephen...@u.tsukuba.ac.jp> wrote:
>>  >
>>  > > If the "keyword arguments in __getitem__" feature is added, .get() is
>>  > > purely redundant.  (Of course this thread would then become "make
>>  > > 'default' a standard keyword argument for mutable collections.")
>>  > >
>>  >
>>  > Is that something people want to do? Do people want to be able to
>>  > write `my_dict[key, default=0]` instead of `my_dict.get(key, 0)`?
>>  > What about `my_dict[key, default=None]` instead of `my_dict.get(key)`?
>>
>> Of course they don't want to do that.  Of course dict.get is going
>> nowhere.  Of course that's redundant.  Of course if collections get a
>> standard 'default' for __getitem__, many people *will* start writing
>> `my_dict[key, default=None]`, if only because they don't read enough
>> docs to know about dict.get.
>>
>
> OK, I'll try again. Do people want collections to get a standard 'default'
> for `__getitem__`?
>
> I don't want mappings to grow a second way to do the same thing, and I
> don't want sequences to have a different way to do it from mappings.
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/4QYQNSNUIJ2WRFHTRZKUM2I527CGD7OU/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NHJGJCRLSJQ4RHANVX4KZCISH4PPUOE2/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to