On Wed, Aug 26, 2020 at 11:30 AM Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:
> On 27/08/20 12:53 am, Steven D'Aprano wrote: > > > 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. > Can you elaborate on this for my understanding? > > 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 > I am interested in this proposal, but I am pretty sure that last goal isn't entirely possible, it is only mostly possible. I believe that the way python handles the comma-separated expression inside the [ ] operator is just due to operator precedence, as Steve pointed out previously. So without a major change we cannot write code where this test suite fully passes (three part test): # TEST SUITE 1 assert q[1,] == f(1,) # part 1 assert q[1] == f(1) # part 2 assert q[1,] == f((1,)) # part 3 The only way around this would be for the function, f, to "know" about the presence of hanging commas. Similarly, without such "runtime comma detection" we cannot make it possible for this test suite to fully pass: # TEST SUITE 2 assert q[1,2] == f(1,2) # part 1 assert q[(1,2)] == f((1,2)) # part 2 assert q[(1,2),] == f((1,2),) # part 3 Can we? --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler
_______________________________________________ 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/DASYPSTG2GIO3DENXO4R5QAKP6264L7Y/ Code of Conduct: http://python.org/psf/codeofconduct/