[Python-ideas] slice.literal notation

Joseph Jevnik joejev at gmail.com
Thu Jun 11 01:45:34 CEST 2015


We cannot use a class method here because slice.literal(:) is a syntax
error.

On Wed, Jun 10, 2015 at 7:43 PM, Terry Reedy <tjreedy at udel.edu> wrote:

> On 6/10/2015 11:33 AM, Joseph Jevnik wrote:
>
>  I often find that when working with pandas and numpy I want to store
>> slice objects in variables to pass around and re-use; however, the
>> syntax for constructing a slice literal outside of an indexer is very
>> different from the syntax used inside of a subscript. This patch
>> proposes the following change:
>>
>>      slice.literal
>>
>> This would be a singleton instance of a class that looks like:
>>
>> class sliceliteral(object):
>>      def __getitem__(self, key):
>>          return key
>>
>
> Alternate constructors are implemented as class methods.
>
> class slice:
>     ...
>     @classmethod
>     def literal(cls, key):
>         if isinstance(key, cls):
>             return key
>         else:
>             else raise ValueError('slice literal mush be slice')
>
> They are typically names fromxyz or from_xyz.
>
> Tal Einat pointed out that not all keys are slices
>
> > [0] -> int
> > [...] -> Ellipsis
> > [0:1, 2:3] -> 2-tuple of slice object
>
> I think the first two cases should value errors. The third might be
> debated, but if allowed, this would not be a slice constructor.
>
> --
> Terry Jan Reedy
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150610/8d900679/attachment.html>


More information about the Python-ideas mailing list