M.-A. Lemburg writes: > On 27.08.2020 17:53, David Mertz 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] if len(mylist) >- N-1 else None > > > > Neither of those is impractical, but in both cases .get(key) seems to > > express the intent in a more obvious way. > > Really ?
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. Second I agree that ".get" is easier to read and understand (especially with explicit default, as you advocate). It's even more stark if you're after a component of the list element: val = mylist[N if len(mylist) > N else default].content vs. val = mylist.get(N, default).content > Let me try again :-) > > Getting an item from a bag of unknown items can easily fail, > because you don't what's in the bag until you try to find > the item. > > Trying to access an item from a non-existing bucket in an array > will always fail. You know that in advance, since the bucket > doesn't exist as per the array properties. You don't have to > try finding it first. But this isn't a C array. You don't know the length from the syntax. So you have to "do work" to discover the bucket isn't there before you access -- or you can delegate to the interpreter and catch IndexError. Given how precise IndexError is (it only catches integer arguments that fall outside the range of the array), I don't really see the argument for refusing to encapsulate the try in a .get method, or that its semantics differ from explicit bounds-checking. It seems to me that you're expressing a preference for "look before you leap" over "easier to ask forgiveness than permission" in the case of array indicies, which is a stylistic thing. It's a style I prefer in this case, but I can also see how other people would prefer a different style, one that makes sense to encapsulate in .get. Is there something more than stylistic preference that I'm missing? _______________________________________________ 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/GFJRG7JALYCJNKVQNV3KCHQAADNG7GLV/ Code of Conduct: http://python.org/psf/codeofconduct/