Re: [Python-ideas] slice[] to get more complex slices

On 2018-07-23 11:58, Grégory Lielens wrote:
Not sure slice[1::3] can be done
It can be done. Since "slice" is a class, it would require a metaclass though.
Another solution that nobody has mentioned (as far as I know) is to add additional syntax to the language for that. For example, one could say that (1:3) could be used to construct slice(1, 3) directly. The parentheses are required to avoid confusion with type hints. I'm not a Python language expert, but I don't think that type hints can occur inside parentheses like that.
Jeroen.

On Mon, Jul 23, 2018 at 6:24 AM, Jeroen Demeyer J.Demeyer@ugent.be wrote:
On 2018-07-23 11:58, Grégory Lielens wrote:
Not sure slice[1::3] can be done
It can be done. Since "slice" is a class, it would require a metaclass though.
Another solution that nobody has mentioned (as far as I know) is to add additional syntax to the language for that. For example, one could say that (1:3) could be used to construct slice(1, 3) directly. The parentheses are required to avoid confusion with type hints. I'm not a Python language expert, but I don't think that type hints can occur inside parentheses like that.
If I remember correctly Guido explicitly rejected requests for such syntax. This proposal was intended as a compromise.

On Mon, Jul 23, 2018 at 3:24 AM, Jeroen Demeyer J.Demeyer@ugent.be wrote:
On 2018-07-23 11:58, Grégory Lielens wrote:
Not sure slice[1::3] can be done
It can be done. Since "slice" is a class, it would require a metaclass though.
Since PEP 560 it won't need a metaclass -- it can be implemented as __class_getitem__.

2018-07-23 8:16 GMT-07:00 Guido van Rossum guido@python.org:
On Mon, Jul 23, 2018 at 3:24 AM, Jeroen Demeyer J.Demeyer@ugent.be wrote:
On 2018-07-23 11:58, Grégory Lielens wrote:
Not sure slice[1::3] can be done
It can be done. Since "slice" is a class, it would require a metaclass though.
Since PEP 560 it won't need a metaclass -- it can be implemented as __class_getitem__.
But that brings up the related problem that `slice[]` for creating slice
objects would now conflict with typing syntax. And we might reasonably make `slice` generic in the future: https://github.com/python/typing/issues/159.
-- --Guido van Rossum (python.org/~guido)
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/

23.07.18 18:16, Guido van Rossum пише:
On Mon, Jul 23, 2018 at 3:24 AM, Jeroen Demeyer <J.Demeyer@ugent.be mailto:J.Demeyer@ugent.be> wrote:
On 2018-07-23 11:58, Grégory Lielens wrote: Not sure slice[1::3] can be done It can be done. Since "slice" is a class, it would require a metaclass though.
Since PEP 560 it won't need a metaclass -- it can be implemented as __class_getitem__.
Do you bless using __class_getitem__ for something other than typing?

Humour warning.
Serhiy wrote:
Do you bless using __class_getitem__ for something other than typing?
As in https://perldoc.perl.org/functions/bless.html, perhaps?

I still think that 'operator.subscript' would be valuable to me for all of the same reasons discussed in the previous threads and issues. I don't understand why it was reverted without any serious discussion given that it was already accepted and many people find this useful.
On Mon, Jul 23, 2018 at 2:38 PM, Serhiy Storchaka storchaka@gmail.com wrote:
23.07.18 18:16, Guido van Rossum пише:
On Mon, Jul 23, 2018 at 3:24 AM, Jeroen Demeyer <J.Demeyer@ugent.be mailto:J.Demeyer@ugent.be> wrote:
On 2018-07-23 11:58, Grégory Lielens wrote: Not sure slice[1::3] can be done It can be done. Since "slice" is a class, it would require a metaclass though.
Since PEP 560 it won't need a metaclass -- it can be implemented as __class_getitem__.
Do you bless using __class_getitem__ for something other than typing?
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/

May I propose `slice.L` as in "slice literal":
``` class slice: ... class L: """Slice literal.
slice.L[1:2:3, 1] -> (slice(1, 2, 3), slice(1)) """
@classmethod def __class_getitem__(cls, item): return item ```
On Mon, Jul 23, 2018 at 12:02 PM Joseph Jevnik joejev@gmail.com wrote:
I still think that 'operator.subscript' would be valuable to me for all of the same reasons discussed in the previous threads and issues. I don't understand why it was reverted without any serious discussion given that it was already accepted and many people find this useful.
On Mon, Jul 23, 2018 at 2:38 PM, Serhiy Storchaka storchaka@gmail.com wrote:
23.07.18 18:16, Guido van Rossum пише:
On Mon, Jul 23, 2018 at 3:24 AM, Jeroen Demeyer <J.Demeyer@ugent.be mailto:J.Demeyer@ugent.be> wrote:
On 2018-07-23 11:58, Grégory Lielens wrote: Not sure slice[1::3] can be done It can be done. Since "slice" is a class, it would require a metaclass though.
Since PEP 560 it won't need a metaclass -- it can be implemented as __class_getitem__.
Do you bless using __class_getitem__ for something other than typing?
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
participants (8)
-
George Leslie-Waksman
-
Guido van Rossum
-
Jelle Zijlstra
-
Jeroen Demeyer
-
Jonathan Fine
-
Joseph Jevnik
-
Serhiy Storchaka
-
Todd