On Wed, Aug 26, 2020 at 10:45 AM Alex Hall <alex.mojaki@gmail.com> wrote:
On Wed, Aug 26, 2020 at 7:30 PM Guido van Rossum <guido@python.org> wrote:
But for your convenience you are proposing a problematic change (see posts by others).

I think we've established it's problematic to add it to the Sequence ABC, but in terms of only adding it to specific sequences like lists, I don't know what other posts you're referring to. Why is it problematic?

It would break duck typing -- if you check for IndexError you can accept pretty much any sequence. But if you use .get() you can only work with lists.

This is why we have ABCs in the first place (and static checking). So that you can't add it to Sequence is already a smell. If it's in the Mapping protocol, why isn't it in the Sequence protocol?

There's also the slippery-slope argument brought up earlier -- should it be added to tuple? To range?

What should it do for a negative index?

I also think it negatively affects readability. Today when I see `foo.get(bar)` I assume that (unless the code is full of tricks) foo is a dict or Mapping, and bar a key. But if list also grows a .get() method I have to assume in my head that foo is a list and then bar an int, and I basically have to read the rest of the code twice, once assuming foo is a Mapping, and once assuming it's a list.
 
Maybe I meant overgeneralization instead.

Again, the goal isn't to generalize. I'm not trying to make lists look like dicts. It's not even possible to get close - `if i in x: print(x[i])` does very different things for lists and dicts.
 
The generalization I am talking about here is the assumption that because list and dict both support x[y], and dict also has x.get(y) which returns a default instead of raising, then it would be a good idea to add the latter also to list. And I judge it an overgeneralization because there's more to it than meets the eye.

--
--Guido van Rossum (python.org/~guido)