enhancing slicing

Bengt Richter bokr at accessone.com
Thu Aug 23 16:24:25 EDT 2001


On Thu, 23 Aug 2001 14:24:40 +0200, "Juan Valiño" <juanv at posta.unizar.es> wrote:

>Many times I want to extract a sublist and I have the initial position  p
>plus the length  n.
>So I do:
>v[p:p+n]
>
>Many times the initial position  p  is an expression. Hence, I have to
>repeat it tediously (and with a possible lost of performance) or assign it
>previously to a variable. The last is boring and is a little trick out of
>the logic of the program
>
>My proposal is to introduce a new operator, for instance # (count):
>v[5 # 2]
>Extract 2 elements starting at 5: [ v[5], v[6] ]. Equivalent to v[5:5+2]
>
>v[5 # -2]
>Extract 2 elements ending at (but not including the) 5: [ v[3], v[4] ].
>Equivalent to v[5-2:5]
>
>I think that this little syntactic sugar allows programs more compact and
>readable. Also, I had less "start and end point paranoias". I suppose that
>the implementation is easy and the current programs are not affected at all.
>
'#' is a comment delimiter which would make that usage awful. But I think
   v[5:+=2]
could be possible and also
   v[-=2:5]

I.e.,
   v[a:+=b]
and
   v[-=b:a]
would be equivalent to
   v[a:a+b]
and
   v[a-b:a]
IOW,
   op=slice_parameter
implies
   slice_parameter_value_to_use = the_other_slice_parameter op slice_parameter

This might have interesting (maybe even useful ;-) consequences
for ops other than + and - and might not be that bad to implement, since
op= tokens are distinct.




More information about the Python-list mailing list