On 27.08.2020 14:40, Daniel. wrote:
> 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*

I disagree on the above assessment. I have had such a get() builtin
in mxTools for more than 20 years now and found that I hardly ever
used it:

https://www.egenix.com/products/python/mxBase/mxTools/doc/#_Toc293606201

The reason is simple: unlike for dicts, where you often expect
non-existing items (e.g. in keyword arguments, config files, etc.),
having a missing index position in a list which you parse is
almost always an error.

Now, errors should be clearly marked and handled as such, hence
having a try-except is the better coding strategy.

For those cases, where a list can have a variable
number of entries (e.g. optional arguments, file lists, etc.),
code should clearly branch on list length and then determine the
right strategy to fetch items.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Aug 27 2020)
>>> Python Projects, Coaching and Support ...    https://www.egenix.com/
>>> Python Product Development ...        https://consulting.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               https://www.egenix.com/company/contact/
                     https://www.malemburg.com/
_______________________________________________
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/7PZCZ7TCLB6M6DAFMDDIWRKBA4UE4PSP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to