Slices by length
Rob Cliffe via Python-list <[email protected]>
| Newsgroups | gmane.comp.python.general |
|---|---|
| Message-ID | <[email protected]> |
I quite often find myself writing expressions of the form someString[x : x+n] where n is often an int and x may be an int, a variable, or a (possibly complicated) expression. It would be more natural to be able to specify the slice not by its startpoint and ENDPOINT, but by its startpoint and LENGTH, particularly when x is a long expression and has to be repeated or pre-stored in a variable. I have worked with languages that had this facility, and I miss it. The possible syntaxes are restricted by the need to avoid ambiguity with existing valid code. It could be for example someString[x ! n] (of course, x and n could be arbitrary expressions). One point to be decided would be whether negative n would produce (a) an empty string, so that s[x ! n] was always the same as s[x : x+n] (b) a slice ending at x, e.g. "01234567"[4!-3] would equal "123" (or less plausibly "321"). I don't have a strong opinion on this; there may be good reasons for preferring one to another. Does anybody think this is a good idea? Best wishes Rob Cliffe -- https://mail.python.org/mailman3//lists/python-list.python.org