On Tue, Aug 25, 2020, 7:35 PM Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:
> On 26/08/20 10:03 am, Stefano Borini wrote: > > But you have a point that whatever the implementation might be, it has > > to play nice with the current dict() behavior. Yet, if we were to add > > an enhanced dunder, nothing for the current dict would change. > > Despite arguing against it earlier, I think I'm starting to shift > towards being in favour of a new dunder. But only if it can be > justified as existing in its own right alongside __getitem__, > without any intention to deprecate or remove the latter. > > ... > > The only drawback I can think of right now is that it would make > possible objects for which x[1, 2] and x[(1, 2)] were not equivalent. > I don't think that's a serious problem. A class would always be able to > arrange for its __getindex__ method to *make* them equivalent, and > things like pandas would be encouraged to do so. For things like generic > types, which use the index notation for something that isn't really > indexing at all, it probably doesn't matter much. > > -- > Greg > To be backwards compatible, any proposed change has to pass this test: ############ """Test #1""" class D: def __getitem__(self, key): return key d = D() # note the commas assert d[1,] == f((1,)) ################ Where f is some function that mimicks how python "translates" the "stuff" inside the square brackets. It is something like this, not quite: def f(*args): if not args: # empty brackets not allowed raise SyntaxError() if len(args) ==1: return args[0] return args The desire of many (me included) seems to be to make it possible for the subscript operator to utilize syntax very similar, or identical, to the behavior of a function call. In other words, to make it possible to write some class, Q, that allows the following tests to pass: ################# """Test #2""" # note the commas assert q[1,] == f(1,) assert q[1] == f(1) assert q[(1,)] == f((1,)) ################## The problem is, I don't think it is possible to change python internals in a backwards compatible way so that we can write a function that passes all of these tests. Because the index operator recognizes this as a tuple: 1, But function calls do not. This is an impasse. Does this constitute a big problem for the idea of providing new dunders? I'm not sure.
_______________________________________________ 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/GZPUUEUONQVYRR3QD5ACR62VG3BXYSAJ/ Code of Conduct: http://python.org/psf/codeofconduct/