On Fri, 19 Feb 2021 at 16:27, Tobias Kohn <ko...@tobiaskohn.ch> wrote: > > Quoting Oscar Benjamin <oscar.j.benja...@gmail.com>: > > > I'm not entirely sure but I think that with PEP 653 you can implement this > > like: > > > > def __deconstruct__(obj): > > if obj.step != 1: > > return obj.start, obj.stop, obj.step > > elif obj.start != 0: > > return obj.start, obj.stop > > else: > > return obj.stop > > > > I think that would then mean you could use a pattern range(10) to > > unambiguously match range(10) without matching range(10, 20) etc. > > This is certainly more of an anti-pattern (no pun intended) than an argument > for the match protocol. Shuffling positional arguments around like this > seems like a rather bad idea, particularly in the context of pattern > matching. I'd rather write `case range(_, stop, _)` or `case range(0, stop, > 1)` and be explicit here.
Good point! I was trying to reason out the mechanism but the alternative approach as you say would be to always use all 3 arguments. PEP 653 here actually makes it possible for the range class to affect matching so that it can only succeed against a pattern that uses all 3 arguments. The significance of range as an example is basically that it has a varying number of positional only arguments to its constructor. The most important feature of the example is not the reordering of arguments: it is the fact that with PEP 634 a pattern range(x) would have to match range(x, y) which is very unlikely to be useful in the case of range but also not generally applicable for other types either. There is no way for range (or other types) to prevent that while supporting positional argument matching through __match_args__. Under PEP 634 in general, for any class C, a pattern C(x) matches an object C(x, y) and there's no way for C to override that. To me that is sufficiently unintuitive in the abstract that no example is really needed to see where there is room for improvement. -- Oscar _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/43UTGYJHOEZSMOFA7WHCJ6ERR2JFQPUE/ Code of Conduct: http://python.org/psf/codeofconduct/