<div dir="ltr">+1<div><br></div><div>This is an elegant improvement that doesn't affect backward compatibility.  Obviously, the difference between the spelling 'sliceliteral[::-1]' and 'slice.literal[::-1]' isn't that big, but having it attached to the slice type itself rather than a user class feels more natural.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 10, 2015 at 8:33 AM, Joseph Jevnik <span dir="ltr"><<a href="mailto:joejev@gmail.com" target="_blank">joejev@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I was told in the thread that it might be a good idea to bring this up on python discussions. Here is a link to the proposed patch and some existing comments: <a href="http://bugs.python.org/issue24379" target="_blank">http://bugs.python.org/issue24379</a><br><div><br>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:<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><br><br>The basic idea is to provide an alternative constructor to 'slice' that uses the subscript syntax. This allows people to write more understandable code.<br><br>Consider the following examples:<br><br>reverse = slice(None, None, -1)<br>reverse = slice.literal[::-1]<br><br>all_rows_first_col = slice(None), slice(0)<br>all_rows_first_col = slice.literal[:, 0]<br><br>first_row_all_cols_but_last = slice(0), slice(None, -1)<br>first_row_all_cols_but_last = slice.literal[0, :-1]<br><br><br>Again, this is not intended to make the code shorter, instead, it is designed to make it more clear what the slice object your are constructing looks like.<br><br>Another feature of the new `literal` object is that it is not limited to just the creation of `slice` instances; instead, it is designed to mix slices and other types together. For example:<br><br>>>> slice.literal[0]<br>0<br>>>> slice.literal[0, 1]<br>(0, 1)<br>>>> slice.literal[0, 1:]<br>(0, slice(1, None, None)<br>>>> slice.literal[:, ..., ::-1]<br>(slice(None, None, None), Ellipsis, slice(None, None, -1)<br><br>These examples show that sometimes the subscript notation is much more clear that the non-subscript notation.<br>I believe that while this is trivial, it is very convinient to have on the slice type itself so that it is quickly available. This also prevents everyone from rolling their own version that is accesible in different ways (think Py_RETURN_NONE).<br>Another reason that chose this aproach is that it requires no change to the syntax to support.<br><br>There is a second change proposed here and that is to 'slice.__repr__'. This change makes the repr of a slice object match the new literal syntax to make it easier to read.<br><br>>>> slice.literal[:]<br>slice.literal[:]<br>>>> slice.literal[1:]<br>slice.literal[1:]<br>>>> slice.literal[1:-1]<br>slice.literal[1:-1]<br>>>> slice.literal[:-1]<br>slice.literal[:-1]<br>>>> slice.literal[::-1]<br>slice.literal[::-1]<br><br>This change actually affects old behaviour so I am going to upload it as a seperate patch. I understand that the change to repr much be less desirable than the addition of 'slice.literal'<br></div></div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">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></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons.  Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div>