You know, it might be nice to have a recipe that creates a view to any abc.Sequence for when you know that the underlying sequence won't change (or don't care). Something like:
class View: ...
some_view = View("some string", slice(2, 5))
some_view[0: 2] "me"
Also a MutableView class could be used for abc.MutableSequences.
On Sat, May 9, 2015 at 1:06 AM, Chris Angelico email@example.com wrote:
On Sat, May 9, 2015 at 2:04 PM, Nikolaus Rath Nikolaus@rath.org wrote:
On May 07 2015, Steven D'Aprano steve-iDnA/YwAAsAk+I/ owrrOrA@public.gmane.org wrote:
But a view would be harmful in this situation:
s = "some string"*1000000 t = s[1:2] # a view maskerading as a new string del s
Now we keep the entire string alive long after it is needed.
How would you solve the first problem without introducing the second?
Keep track of the reference count of the underlying string, and if it goes down to one, turn the view into a copy and remove the sliced original?
Oops, mis-sent (stupid touchpad on this new laptop). Trying again.
There might be multiple views, so a hard-coded refcount-of-one check wouldn't work. The view would need to keep a weak reference to its underlying string - but not in the sense of the Python weakref module, which doesn't seem to have any notion of "about to be garbage collected", but only "has now been garbage collected". Notably, by the time a callback gets called, it's too late to retrieve information from the callback itself. A modified form of weakref could do it, though; with the understanding that the referents are immutable, and premature transform from view to coalesced slice has no consequence beyond performance, this could be done.
Ideally, it'd be an entirely invisible optimization.
Python-ideas mailing list Pythonfirstname.lastname@example.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group. To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/II-4QRDb8Is/unsubscribe. To unsubscribe from this group and all its topics, send an email to email@example.com. For more options, visit https://groups.google.com/d/optout.