On 2020-08-28 at 08:41:04 -0400,
Regarding "[Python-ideas] Re: What about having a .get(index, default) method 
for arrays like we have for dicts?,"
Richard Damon <rich...@damon-family.org> wrote:

> On 8/28/20 8:24 AM, David Mertz wrote:
> >
> > As a side note, I don't really get why everyone else thinks a
> > try/except is the most natural approach while a ternary seems more
> > obvious to me for this situation.  But it kinda connects to me liking
> > list.get() better, I think... since "not enough items" doesn't seem as
> > *exceptional* to me as it apparently does to some others.
> 
> try/except is specifically looking for the error in question, index out
> of bounds. The ternary requires restating in other words the bounds limit.
> 
> For instance, for a list of N items, the allowable subscripts are -N to
> N-1, and the sample ternary is only checking for the upper bound, not
> the lower, so fails an too small of an index (maybe you know you are
> only using positive indexes?) Missing this won't happen with try/except.

Without more context, I've lost the forest for the trees.  There are
multiple ways to do this, and the "best" way likely depends on the
context and not the readability, writability, maintainability, or
expressivity of that exact line of code.

Where did the index come from?  How/when/where was the list constructed?
Could this "problem" have been recognized and/or addressed sooner?
What, exactly, are the conditions that cause the index being out of
bounds at this point in the logic, and what are the semantics now that
it's happened?  Given the preceding logic, is an index that's too small
(i.e. mathematically less than the opposite of the lenght of the list)
even possible?

I didn't find either of the examples (one using an if/else ternary and
one using a hypothetical list.get method) particulary easy to read or
particularly indicative of an obvious use case.  And at that point,
re-writing one [presumably correct] line of code into another won't make
or break anything.
_______________________________________________
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/BG2P6BCSDDANXU2XR4XU75O2AOLRVHM5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to