3 ms·
> This raises the cost of functions like "find()", which search the string for something and return an integer which is a position in the string suitable for ra
by bjourne 8y ago
> This raises the cost of functions like "find()", which search the string for something and return an integer which is a position in the string suitable for random access use. I'd once suggested that "find", etc. return an opaque object which was really a byte index into the string.
Good idea, but it can lead to some hard to handle special cases. Consider for example running find() on a number of large strings and storing the opaque results in a list. Since the objects would require the strings to evaluate the opaque objects Python wouldn't be able to garbage collect them, potentially leading to hard-to-track-down and unexpected out-of-memory errors. One could mitigate the problem by using a buffer to limit the size of the strings kept alive in this way, but eh, it gets complicated. :)
- swsieber 8y agoYou wouldn't have to use an opaque object - you could use an object that contains both the character index and the byte index, which the indexing operation would understand how to use.