On Wed, Jun 4, 2014 at 8:10 PM, Peter Otten <__pete...@web.de> wrote: > The indices used for slicing typically don't come out of nowhere. A simple > example would be > > def strip_prefix(text, prefix): > if text.startswith(prefix): > text = text[len(prefix):] > return text > > If both prefix and text use UTF-8 internally the byte offset is already > known. The question is then how we can preserve that information.
Almost completely useless. First off, it solves only the problem of operating on the string at exactly some point where you just got an index; and secondly, you don't always get that index from a string method. Suppose, for instance, that you iterate over a string thus: for i, ch in enumerate(string): if ch=='{': start = i elif ch=='}': return string[start:end+1] Okay, so this could be done by searching, but for something more complicated, I can imagine it being better to enumerate. (But "I can imagine" is much weaker than "Here's code that we use in production", which is why I asked the question.) Incidentally, the above code highlights the first problem too. With direct indexing, you can ask for inclusive or exclusive slicing by adding or subtracting one from the index. If you do that with a byte-position-retaining special integer, you lose the byte position. ChrisA -- https://mail.python.org/mailman/listinfo/python-list