<div dir="ltr">We cannot use a class method here because slice.literal(:) is a syntax error.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 10, 2015 at 7:43 PM, Terry Reedy <span dir="ltr"><<a href="mailto:tjreedy@udel.edu" target="_blank">tjreedy@udel.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 6/10/2015 11:33 AM, Joseph Jevnik wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I often find that when working with pandas and numpy I want to store<br>
slice objects in variables to pass around and re-use; however, the<br>
syntax for constructing a slice literal outside of an indexer is very<br>
different from the syntax used inside of a subscript. This patch<br>
proposes the following change:<br>
<br>
     slice.literal<br>
<br>
This would be a singleton instance of a class that looks like:<br>
<br>
class sliceliteral(object):<br>
     def __getitem__(self, key):<br>
         return key<br>
</blockquote>
<br></span>
Alternate constructors are implemented as class methods.<br>
<br>
class slice:<br>
    ...<br>
    @classmethod<br>
    def literal(cls, key):<br>
        if isinstance(key, cls):<br>
            return key<br>
        else:<br>
            else raise ValueError('slice literal mush be slice')<br>
<br>
They are typically names fromxyz or from_xyz.<br>
<br>
Tal Einat pointed out that not all keys are slices<span class=""><br>
<br>
> [0] -> int<br>
> [...] -> Ellipsis<br>
> [0:1, 2:3] -> 2-tuple of slice object<br>
<br></span>
I think the first two cases should value errors. The third might be debated, but if allowed, this would not be a slice constructor.<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Terry Jan Reedy</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br></div>