On 27/08/20 12:53 am, Steven D'Aprano wrote:

Presumably the below method is provided by `object`. If not, what
provides it?

     def __getindex__(self, *args, **kwds):
         if kwds:
             raise TypeError("Object does not support keyword indexes")
         if not args:
             raise TypeError("Object does not accept empty indexes")

It's not literally a method, I just wrote it like that to
illustrate the semantics. It would be done by the interpreter
as part of the process of translating indexing operations into
dunder calls.

What is your reasoning behind prohibiting keywords, when we're right in
the middle of a discussion over PEP 474 which aims to allow keywords?

We're falling back to __getitem__ here, which doesn't currently allow
keywords, and would stay that way. The point of this proposal is to
not change __getitem__. If you want to get keywords, you provide
__getindex__.

         if len(args) == 1:
             args = args[0]
         return self.__getitem__(args)

This is going to slow down the most common cases of subscripting: the
interpreter has to follow the entire MRO to find `__getindex__` in
object, which then dispatches to the `__getitem__` method.

No, it would be done by checking type slots, no MRO search involved.

In your earlier statement, you said that it would be possible for
subscripting to mean something different depending on whether the
comma-separated subscripts had parentheses around them or not:

     obj[(2, 3)]
     obj[2, 3]

How does that happen?

If the object has a __getindex__ method, it gets whatever is between
the [] the same way as a normal function call, so comma-separated
expressions become separate positional arguments.

--
Greg
_______________________________________________
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/A6PUWAGM3FAIWPDCMVZLFUZYZJZ2SK4L/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to