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.
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
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")
if len(args) == 1:
args = args[0]
return self.__getitem__(args)
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.
[1] Or at least not one that we aren't entitled to break.
--
Greg
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/WPIMR75Z5U3JRRN7X56IXZRJK4NRJMBI/
Code of Conduct: http://python.org/psf/codeofconduct/