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

Todd toddrjen at gmail.com
Mon Jul 23 09:00:12 EDT 2018


On Mon, Jul 23, 2018 at 8:41 AM, Paul Moore <p.f.moore at gmail.com> wrote:

> On 23 July 2018 at 13:19, Todd <toddrjen at gmail.com> wrote:
> > On Mon, Jul 23, 2018 at 7:24 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> >>
> >> On 23 July 2018 at 11:31, Jeroen Demeyer <J.Demeyer at ugent.be> wrote:
> >> > On 2018-07-23 12:24, Jeroen Demeyer wrote:
> >> >>
> >> >> 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.
> >> >
> >> >
> >> > And this could be extended to tuples (1:3, 2:4) and lists [1:3, 2:4]
> of
> >> > slices too.
> >>
> >> I thought the reason the proposal got nowhere was because it's pretty
> >> simple to define it yourself:
> >>
> >> >>> class SliceHelper:
> >> ...     def __getitem__(self, slice):
> >> ...         return slice
> >> ...
> >> >>> SH = SliceHelper()
> >> >>> SH[1::3]
> >> slice(1, None, 3)
> >>
> >> Did I miss something significant about why this wasn't sufficient?
> >>
> >> Paul
> >
> >
> > That involves initializing an instance, which doesn't serve any purpose
> in
> > this case and I was hoping to avoid.
>
> Well it serves the purpose that you can do it already in current
> Python, rather than needing a core interpreter change and limiting
> your code to Python 3.8+ only ;-)
>
> Paul
>

Making it a classmethod would only require a metaclass, not a core
interpreter change.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180723/1aeecbba/attachment.html>


More information about the Python-ideas mailing list