[Python-ideas] Where did we go wrong with negative stride?

MRAB python at mrabarnett.plus.com
Thu Oct 31 01:06:03 CET 2013


On 30/10/2013 21:52, Nick Coghlan wrote:
>
> On 31 Oct 2013 07:14, "Greg Ewing" <greg.ewing at canterbury.ac.nz
> <mailto:greg.ewing at canterbury.ac.nz>> wrote:
>  >
>  > Paul Moore wrote:
>  >>
>  >> I would definitely use
>  >>
>  >>     b = a[::-1]
>  >>
>  >> over
>  >>
>  >>     b = a[Slice(None, None, None, reversed=True)]
>  >
>  >
>  > Indeed, the whole reason for having slice syntax is that
>  > it's very concise. One of the things I like most about
>  > Python is that I get to write s[a:b] instead of something
>  > like s.substr(a, b).
>  >
>  > I would be very disappointed if I were forced to use
>  > the above monstrosity in some cases.
>
> You can't have new syntax without defining the desired semantics for
> that syntax first. Since slices are just objects, it doesn't make sense
> to argue about syntactic details until the desired semantics are
> actually clear and demonstrated in an object based proof-of-concept.
>
How about a new function "rev" which returns the reverse of its argument:

def rev(arg):
     if isinstance(arg, str):
         return ''.join(reversed(arg))

     return type(arg)(reversed(arg))

The disadvantage is that it would be slicing and then reversing, so 2
steps, which is less efficient.


More information about the Python-ideas mailing list