On Wed, Aug 26, 2020 at 03:06:25PM +1200, Greg Ewing wrote:
On 26/08/20 1:59 pm, Steven D'Aprano wrote:
Most existing uses of subscripts already don't fit that key:value mapping idea, starting with lists and tuples.
Not sure what you mean by that.
Lists and tuples aren't key:value mappings.
Given `obj[spam]`, how does the interpreter know whether to call `__getitem__` or `__getindex__`? What if the class defines both?
If it has a __getindex__ method, it calls that using normal function parameter passing rules. Otherwise it uses a fallback something like
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")
Empty subscripts will remain a syntax error, so I don't think this case is possible. 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?
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. It would make more sense to put the logic in the byte-code, rather than `object`: if the object supports __getindex__ call it otherwise call __getitem__ which is closer to how other operators work, and avoids giving object a spurious dunder that it doesn't use. However it still has to check the MRO, so I don't know if that actually gains us much performance. The fundamental issue with this proposal is that, as far as I can see, it solves no problems and offers no new functionality. It just adds complexity. As Stephan Hoyer has said, the existing handling of commas in subscripts is not a problem for Numpy. I doubt it is a problem for Pandas either. Is it a problem for anyone?
Right now, both sets of syntax mean the same thing and call the same method, so you are introducing a backwards incompatible change that will break code.
No existing object will have a __getindex__ method[1], so it won't change any existing behaviour.
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? I thought I understood what you meant by that, but I obviously didn't. Can you explain how they would be different? I understood that you meant that the intepreter would choose which dunder to call according to whether or not there are parentheses around the items, but now I'm not sure what you meant. -- Steve