On Fri, Aug 28, 2020 at 4:30 AM Stephen J. Turnbull < turnbull.stephen...@u.tsukuba.ac.jp> wrote:
> > > Suppose we didn't have dict.get(). I would then probably write: > > > val = mydict[key] if key in mydict else None > > > > > > Likewise, since we don't have list.get(), I would write: > > > val = mylist[N-1] if len(mylist) >= N else None > > I think David is comparing the .get syntax to the if-else syntax. > First of all, he got the if-else wrong, which is a sign that .get is > easier to use, at least for writers and quite likely for readers. > It was an unintentional mistake, but Stephen is right it makes the point. I fixed above, both my thinko reversing the N vs. N-1, and also the simple typo of `>-` instead of `>=`. No language change is going to fix the fact the '-' and '=' keys are next to each other on my keyboard. I think the comparison (with right syntax and semantics) is fair. Whatever the ontological significance MAL notes about "reaching into the void" versus "reaching into the dark", the code I would actually write if dict.get() didn't exist is pretty much the above. And it looks a whole lot like the code I actually do write for possibly-missing index positions. 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. -- The dead increasingly dominate and strangle both the living and the not-yet born. Vampiric capital and undead corporate persons abuse the lives and control the thoughts of homo faber. Ideas, once born, become abortifacients against new conceptions.
_______________________________________________ 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/ZEPTEIJUVEMYV2EECWO67JBBXTZHWXHV/ Code of Conduct: http://python.org/psf/codeofconduct/