[Python-ideas] slice.literal notation
Terry Reedy
tjreedy at udel.edu
Thu Jun 11 01:43:13 CEST 2015
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
More information about the Python-ideas
mailing list