Em qui, 27 de ago de 2020 10:09, M.-A. Lemburg <m...@egenix.com> escreveu:
> 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. > Almost always an error is not always an error and errors are not always exceptions, a missing index maybe something expected. > > 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. > Branching has the same effect of try/catch, it will break code flow and make it more complex, so less readable. .get() is an nice abstraction to get something or return a default, well understood, it cant be less readable. I I came across this many times, many enought to come here to ask, and the use cases we're already showed, so I don't think, is useless would be a good argument. I understand that people disagreed on what is readable, but not that it rarely needed > > -- > 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/XIMTNBGHUX6YXKXRWCEFT2F5SX3OKDQ3/ Code of Conduct: http://python.org/psf/codeofconduct/