[Python-3000] Making more effective use of slice objects in Py3k (original) (raw)
Josiah Carlson jcarlson at uci.edu
Mon Aug 28 10:00:55 CEST 2006
- Previous message: [Python-3000] Making more effective use of slice objects in Py3k
- Next message: [Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
Josiah Carlson wrote: > If one wants a view of text, one needs to manually > construct the view via 'view = textview(st, start, stop)' or some > equivalent spelling. After that, any operations on a view returns views
Given Guido's sensitivity about potential misuses of views, it might be better if operations on views didn't return views, so that you would have to be explicit about creating views at all stages.
If every operation on a view returned a string copy, then what would be the point of the view in the first place? An alias for Python 2.x buffer()? No, that would be silly.
As I see it, the point of string/text views is:
- Remove all start, stop optional arguments from all string methods, replacing them with view slicing; resulting in generally improved call performance by the second or third operation on the original string.
- Reduce memory use and fragmentation of common operations (like... while rest: prev, found, rest = rest.partition(sep) ) by performing those operations on views.
- Reduce execution time of slicing or slicing-like operations by performing them on views (prev, found, rest = rest.partition(sep)).
Note that with 2 and 3, it doesn't matter how much or little you 'slice' from the view, the slicing and/or creation of new views referencing the original string is a constant time operation every time.
By making view.oper() always return strings instead of views, it makes #1 the only reason for views, even though #2 and #3 are also important and valid motivators.
I would also like to point out that it would make the oft-cited partition example "while rest: first, found, rest = rest.partition(sep)" run in linear rather than quadratic time, where users will be pleasantly surprised about improvement in speed (or the lack of a reduction in speed).
- Josiah
- Previous message: [Python-3000] Making more effective use of slice objects in Py3k
- Next message: [Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]